File size increases when converting an html file to adobe pdf using google apps script

。_饼干妹妹 提交于 2021-02-08 06:45:48

问题


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);

}

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.

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

My test results are as follows:

  • Converting the sample.html file with Google Apps Script causes the 133 KB HTML file to become a 10 MB PDF.
  • Converting the sample.html file with Adobe Acrobat XI Pro causes the 133 KB HTML file to become a 151 KB PDF.

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?

Thanks in advance for any help with this!


回答1:


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});

}

And I also get 10MB size from this code.



来源:https://stackoverflow.com/questions/38753578/file-size-increases-when-converting-an-html-file-to-adobe-pdf-using-google-apps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!