使用谷歌应用程序脚本将 html 文件转换为 adobe pdf 时文件大小增加 [英] File size increases when converting an html file to adobe pdf using google apps script

查看:23
本文介绍了使用谷歌应用程序脚本将 html 文件转换为 adobe pdf 时文件大小增加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,在 Google Apps Script 中将 HTML 文件转换为 PDF 时,PDF 的文件大小最终比原始 HTML 文件大得多.

For some reason, when converting an HTML file to a PDF in Google Apps Script, the file size of the PDF ends up being way bigger than the original HTML file.

我用来运行测试的代码如下:

The code I used to run the test is below:

function convertHTMLtoPDF() {
// convert a specific message with an attached HTML file to PDF,
// and save it to the root directory of Google Drive
  var threads = GmailApp.search('subject:(Sample HTML)');
  var message = threads[0].getMessages()[0];

  var attachmentName = message.getAttachments()[0].getName();

  var attachment = message.getAttachments()[0].getAs("application/pdf");
  DriveApp.createFile(attachment).setName(attachmentName);

}

注意:您只需要获取我在下面共享的示例 HTML 文件,将其附加到主题为:示例 HTML 的电子邮件中,然后将其发送给您自己,以便脚本找到它.

NOTE: You just need to take the sample HTML file I'm sharing below, attach it to an email with the subject: Sample HTML, and send it to yourself to allow the script to find it.

我正在转换的 示例 HTML 文件为 133 KB.脚本运行后,转换后的 为 10 MB.

The sample HTML file I was converting is 133 KB. The converted is 10 MB after the script is run.

我的测试结果如下:

  • 使用 Google Apps 脚本转换 sample.html 文件会使 133 KB HTML 文件变成 10 MB PDF.
  • 使用 Adob​​e Acrobat XI Pro 转换 sample.html 文件会使 133 KB HTML 文件变成 151 KB PDF.

有谁知道为什么通过 Google Apps Script 转换的 PDF 文件大小比使用 Adob​​e 转换时大得多?有没有办法减小文件大小?

Does anyone know why the file size of the PDF that is converted through Google Apps Script is so much bigger than when converting with Adobe? Is there a way to reduce the file size?

在此先感谢您的帮助!

推荐答案

我试过你的代码,我确认大小变大了(10 MB).我认为这是很自然的,因为与文件的原始类型相比,PDF 文件总是很大.您可以在互联网上找到许多可以解释这一点的原因.一个例子是这个.

I tried your code and I confirmed that the size became larger(10 MB). I think this is natural because PDF file is always large compared to the original type of the file. You can find many reasons in the internet that can explain that. One example is this.

为了确认这一点,我使用了这个脚本来确认大小.

To confirm that, I used this script to confirm that size.

function htmlToPDF() {

  var html = "YOUR HTML CODE";

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

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

  MailApp.sendEmail("sample@email.org", "PDF File", "", 
     {htmlBody: html, attachments: pdf});

}

我也从这段代码中获得了 10MB 大小.

And I also get 10MB size from this code.

这篇关于使用谷歌应用程序脚本将 html 文件转换为 adobe pdf 时文件大小增加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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