Show images inside a pdf created with Gloogle Apps Script Blob

徘徊边缘 提交于 2019-11-29 17:35:18

If the images in HTML are loaded from URLs, the converted PDF file doesn't include the images. If you want to include the images in the converted PDF file, please put the images to HTML as base64 data.

In order to confirm this situation, please see the following sample script. This sample script puts an image as URL and base64 data. The result is displayed them to a dialog and created a PDF file.

When you use this script, please copy and paste to the script editor of Spreadsheet.

Sample script :

function myFunction() {
  // image
  var url = "https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-icon.png?v=c78bd457575a";
  var blob = UrlFetchApp.fetch(url).getBlob();
  var b64 = blob.getContentType() + ';base64,'+ Utilities.base64Encode(blob.getBytes());
  var html = "URL<img src=\"" + url + "\">Base64<img src=\"data:" + b64 + "\">";
  var h = HtmlService.createHtmlOutput(html);

  // Open dialog with the images.
  SpreadsheetApp.getUi().showModalDialog(h.setWidth(500).setHeight(200), 'Sample');

  // Create from HTML to PDF file.
  DriveApp.createFile(h.getAs("application/pdf").setName("text.pdf"));
}

Result of dialog :

Both images can be seen.

Result of PDF file :

Only the image of base64 can be seen.

Note :

  • This is a simple script. So please modify this to your environment.

References :

If I misunderstand your question, I'm sorry.

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