简体   繁体   English

在网页中添加打印为 pdf 选项

[英]Add Print as pdf option in web page

I want to print the image shown below as a PDF by providing a print option on my web page, I have the image path and other details that I want in the pdf such as date, description etc on the database.我想通过在我的网页上提供打印选项来将下面显示的图像打印为 PDF,我在 pdf 中有我想要的图像路径和其他详细信息,例如数据库中的日期、描述等。 I want to print the image by providing the URL and details that I can get from the database.我想通过提供可以从数据库中获取的 URL 和详细信息来打印图像。 Thank you谢谢在此处输入图片说明

jspdf seems to be ok for your requirements, you can build pdf file containing image. jspdf似乎可以满足您的要求,您可以构建包含图像的 pdf 文件。 It is quite simple in fact :其实很简单:

var doc = new jsPDF();
doc.setFontSize(40);
doc.addImage(imgData, 'JPEG', 15, 40, 180, 160);

The only problem here from my understanding is that your have to get image from url then convert it to base64.根据我的理解,这里唯一的问题是您必须从 url 获取图像,然后将其转换为 base64。

Here is a working solution :这是一个有效的解决方案:

https://codepen.io/bcaure/pen/OEVyKB https://codepen.io/bcaure/pen/OEVyKB

 var xmlHTTP = new XMLHttpRequest(); xmlHTTP.open('GET', 'https://gravatar.com/avatar/09e327bdf4e6fc03bdda0fbaccf18132', true); xmlHTTP.responseType = 'blob'; xmlHTTP.onload = function() { var reader = new FileReader(); reader.readAsDataURL(xmlHTTP.response); reader.onload = function(url){ var doc = new jsPDF(); doc.addImage(url.target.result, 'JPEG', 15, 40, 180, 160); doc.save('two-by-four.pdf'); }; }; xmlHTTP.send();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM