Google脚本-从text/html转换为application/pdf失败 [英] Google Script - Conversion from text/html to application/pdf failed
问题描述
我有一个带有简单逻辑的自定义Google脚本:它从电子表格中查找零件并构建内存HTML字符串,然后调用以下代码来生成PDF.
I have a custom Google Script with a simple logic: it looks up parts from the spreadsheet and builds an in-memory HTML string, then I call the following code to produce the PDF.
var blob = Utilities.newBlob(html_body, 'text/html').getAs('application/pdf').setName(pdf_name);
var newDoc = DriveApp.createFile(blob);
var pdf_url = newDoc.getUrl();
令人惊讶的是,对于相同的内容,它有时可以工作并随机返回错误:
Surprisingly for the same content, it sometimes works and randomly returns the error:
从text/html到application/pdf的转换失败.
Conversion from text/html to application/pdf failed.
我看到较大的文件会发生这种情况,因此我可能会遇到一些配额限制.
I see this happening most for larger files, so I am probably hitting some quota limitations.
但根据 https://developers.google.com/apps-script/guides/services/quotas#current_limitations 遇到任何限制都应引起相关提示.
But as per https://developers.google.com/apps-script/guides/services/quotas#current_limitations hitting any limitations should raise a relevant message.
有关 https://developers.google的文档.com/apps-script/reference/base/blob#getAs(String)未声明任何内容.
有什么想法吗?
谢谢!
推荐答案
我遇到了同样的问题,并且执行了以下操作:
I had the same problem, and did the following:
var html = HtmlService.createHtmlOutput(html_body);//This is now an object
html = html.getContent();//Get data as string - So its not an object or file
var blob = Utilities.newBlob(html,'application/pdf');//Create pdf from string
blob.setName(pdf_name);
就我而言,getAs('application/pdf')
方法无法将Blob从文本文件转换为pdf文件.
In my case, the getAs('application/pdf')
method was failing to convert a blob from a text file to a pdf file.
这篇关于Google脚本-从text/html转换为application/pdf失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!