将Blob从HTML转换为PDF时未复制的图像 [英] Images not reproduced when converting a Blob from HTML to PDF

查看:341
本文介绍了将Blob从HTML转换为PDF时未复制的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将HTML电子邮件转换为PDF。我写了下面这段代码。

  var txt = msgs [i] .getBody(); 
/ *我们需要两个blob转换 - 一个从文本到HTML,另一个从HTML到PDF * /
var blob = Utilities.newBlob(txt,'text / html','Test PDF') ;
Logger.log(txt);
var tempDoc = DocsList.createFile(blob);
var pdf = tempDoc.getAs('application / pdf');
pdf.setName('Email As PDF');
DocsList.createFile(pdf);

上面这段代码首先从Gmail邮件中创建一个Blob,并使用getAs ()函数将其转换为PDF。但是,PDF中不能找到HTML中的图像。任何想法如何获得这些图像将不胜感激。
有关如何将gmail邮件转换为PDF的任何替代想法也很受欢迎。

解决方案

有趣的问题。有道理,为什么这不起作用 - PDF转换不会渲染HTML来获取图像src。

我做了一个快速测试,并确认数据URI的(不需要单独的HTTP调用的内联图像)与图像一起工作。

因此,一个hacky解决方案可能会去获取图像,然后将它们转换为数据URI。这有几个缺点 - 很难找到这些图像(正则表达式会很脆弱或不全面),很多UrlFetch调用(即使有一些缓存,大多数自动电子邮件发件人添加跟踪器,以便最终重新获取相同的图像)和慢。

转换 -



< img src =http:// images。 (您也可以动态检查内容类型) - $ b

/ p>

< img src =data:image / png; base64,iVBORw0KGgoAAAANSUhE .../>


I want to convert HTML emails into a PDF. I have written the following piece of code.

      var txt = msgs[i].getBody();
      /* We need two blob conversions - one from text to HTML and the other from HTML to PDF */
      var blob = Utilities.newBlob(txt, 'text/html',"Test PDF");
      Logger.log(txt);
      var tempDoc = DocsList.createFile(blob);
      var pdf = tempDoc.getAs('application/pdf');
      pdf.setName('Email As PDF');
      DocsList.createFile(pdf);

The above piece of code first creates a Blob out of the HTML from a Gmail message and uses the getAs() function to convert it to a PDF. However, images in the HTML are not to be found in the PDF. Any ideas on how to get these images would be appreciated. Any alternative ideas on how to convert a gmail message to PDF is also welcome.

解决方案

Interesting problem. Makes sense as to why this doesn't work - PDF conversion doesn't bother "rendering" the HTML to go fetch the image src.

I did a quick test and confirmed that Data URI's (inline images without requiring a separate HTTP call) worked with images.

So, one hacky solution could be go fetch the images and then convert them to Data URI. This has a few downsides - hard to find these images (regex would be fragile or not comprehensive), lots of UrlFetch calls (even with some caching, most automated email senders add trackers so that you end up re-fetching the same image) and slow.

Convert -

<img src="http://images.myserver.com/myimage.png..."/>

To (you can check the content type dynamically as well)-

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhE..."/>

这篇关于将Blob从HTML转换为PDF时未复制的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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