在使用Gloogle Apps脚本Blob创建的pdf内显示图像 [英] Show images inside a pdf created with Gloogle Apps Script Blob

查看:123
本文介绍了在使用Gloogle Apps脚本Blob创建的pdf内显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用HTML代码在Google Apps脚本中使用Blob创建PDF文件,但是问题是HTML代码具有图片(由"http"引用),但是创建的pdf无法显示它.

I am creating PDF files using blobs in Google Apps Script from a HTML code, but the problem is that HTML code has an image (referenced by "http") but the created pdf can't show it.

这是我的代码

function createBlobPDF(myMessage,myTitle){
var blobHTML = Utilities.newBlob(myMessage, "text/html", myTitle+ ".html");
var myPDF = blobHTML.getAs("application/pdf");
return myPDF;
}

有解决方案吗?谢谢!

推荐答案

如果从URL加载HTML中的图像,则转换后的PDF文件将不包含这些图像.如果要将图像包括在转换后的PDF文件中,请将图像作为base64数据放入HTML.

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.

为了确认这种情况,请参阅以下示例脚本.此示例脚本将图像作为URL和base64数据放置.结果显示在对话框中,并创建PDF文件.

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.

使用此脚本时,请复制并粘贴到Spreadsheet的脚本编辑器中.

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

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.

只能看到base64的图像.

Only the image of base64 can be seen.

  • 这是一个简单的脚本.因此,请根据您的环境进行修改.

如果我误解了你的问题,对不起.

If I misunderstand your question, I'm sorry.

这篇关于在使用Gloogle Apps脚本Blob创建的pdf内显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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