从PDF中的HTML背景色创建PDF [英] Creating PDF from HTML background color in the PDF

查看:40
本文介绍了从PDF中的HTML背景色创建PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了一些有关从Blob创建带有彩色背景示例的PDF的类似问题,但是没有一个答案解决背景色问题",并且我无法使生成的PDF具有任何背景色.

I have seen several similar questions regarding creating PDF from Blob with examples of colored backgrounds, but none of the answers address the "Background color issue" and I can't get the resulting PDF to have any background color.

我遵循了此示例(

I followed this example (Create PDF from HTML in Google Apps Script and include images - Images not showing up) and got the image to be included in the pdf, but the background colors are not in the pdf.

function htmlToPDF() {

  var html = "<h1>Hello world</h1>"
       + "<p>This is just a paragraph"
       + "<p style='color:red;'>I am red</p>"
       + "<p style='color:blue;'>I am blue</p>"
       + "<p style='font-size:50px;'>I am big</p>"
       + "<table style='border-collapse: collapse; width: 698px; height: 115px; background-color: #C5D9F1;' border='0' cellpadding='10'>"
       + "<tbody>"
       + "<tr>"
       + "<td style='padding: 5px;background-color:powderblue;' nowrap='nowrap'><strong>Bold with background-color:</strong></td>"
       + "</tr>"
       + "</tbody>"
       + "</table>";

  var blob = Utilities.newBlob(html, "text/html", "text.html");
  var pdf = blob.getAs("application/pdf");

  DriveApp.createFile(pdf).setName("text.pdf");

  MailApp.sendEmail("[your email here ]", "PDF File", "", 
     {htmlBody: html, attachments: pdf});
}

字体颜色正确,但没有背景色

Font colors are correct, but no background color

推荐答案

答案很简单...发现这是网上冲浪并添加了...

The answer was quite simple... found this surfing the net and added...

    html { -webkit-print-color-adjust: exact; }

这是现在可以使用的代码...

This is the code that now works...

function htmlToPDF() {

  var html = 
      "<style> html { -webkit-print-color-adjust: exact; } </style>"
      + "<h1>Hello world</h1>"
      + "<p>This is just a paragraph"
      + "<p style='color:red;'>I am red</p>"
      + "<p style='color:blue;'>I am blue</p>"
      + "<p style='font-size:50px;'>I am big</p>"
      + "<table style='border-collapse: collapse; width: 698px; height: 115px; background-color: #C5D9F1;' border='0' cellpadding='10'>"
      + "<tbody>"
      + "<tr>"
      + "<td style='padding: 5px;background-color:powderblue;' nowrap='nowrap'><strong>Bold with background-color:</strong></td>"
      + "</tr>"
      + "</tbody>"
      + "</table>";

   var blob = Utilities.newBlob(html, "text/html", "text.html");
   var pdf = blob.getAs("application/pdf");

   DriveApp.createFile(pdf).setName("text.pdf");

   MailApp.sendEmail("[your email here ]", "PDF File", "", {htmlBody: html, attachments: pdf});
}

这篇关于从PDF中的HTML背景色创建PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆