使用HTML文件输出PDF [英] Using HTML file to output a PDF

查看:148
本文介绍了使用HTML文件输出PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个HTML文件,用于发送电子邮件,但在某些情况下,我只是想使用该文件来创建相同模板的PDF。

我的功能大部分都在运行 - 它创建文件,运行评估并获取内容,但实际上并未呈现html。它只是留下所有的html符号。



例如,它输出一个pdf,但它显示为:

 亲爱的Martin,< br /> 

而非:


亲爱的Martin


如何确保呈现HTML以便PDF正确布局, t

以下是代码:

  var docName =test; 
var htmlBody = HtmlService.createHtmlOutput(template.evaluate()。getContent())。getContent()
var doc = DocumentApp.create(docName);
doc.appendParagraph(htmlBody);
doc.saveAndClose();
DocsList.createFile(doc.getAs('application / pdf'))。rename(docName);


解决方案

您可以使用现有的HTML作为blob,将它转换为PDF格式如下:

  var htmlBody = HtmlService.createHtmlOutputFromFile('my_file_within_script_project.html')。getContent(); 
var blob = Utilities.newBlob(htmlBody,'text / html')。getAs('application / pdf')。setName('my_output_in_drive.pdf');
DriveApp.createFile(blob);


So I've got a HTML file, that I am using to send emails, but in some instances I want it simply to use that file to create a PDF of the same template.

I've got it functioning for the most part - it creates the file, runs the evaluations and gets the content, but it doesn't actually render the html. It simply leaves all the html notation in place.

For example, it outputs a pdf but it reads:

Dear Martin, <br />

instead of:

Dear Martin

How do I make sure it renders the HTML so that the PDF is laid out correctly, and doesn't have the html code noted in the text?

Here's the code:

var docName = "test";
var htmlBody = HtmlService.createHtmlOutput(template.evaluate().getContent()).getContent()
var doc = DocumentApp.create(docName);
  doc.appendParagraph(htmlBody);
doc.saveAndClose();
DocsList.createFile(doc.getAs('application/pdf')).rename(docName);

解决方案

You can use your existing HTML as a blob, and convert it to PDF like this:

var htmlBody = HtmlService.createHtmlOutputFromFile('my_file_within_script_project.html').getContent();
var blob = Utilities.newBlob(htmlBody, 'text/html').getAs('application/pdf').setName('my_output_in_drive.pdf');
DriveApp.createFile(blob);

这篇关于使用HTML文件输出PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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