[英]how can i convert a png to pdf using JavaScript in vue.js with node version 10
I have tried using jspdf but it works with node version >=12.我曾尝试使用 jspdf,但它适用于节点版本 >=12。 so i used only canvas instead.
所以我只使用了 canvas 代替。 here when i do canvas.toBuffer, it says it is not a function.
在这里,当我执行 canvas.toBuffer 时,它说它不是 function。
so far my code is:到目前为止,我的代码是:
const canvas = createCanvas(2480, 3508, "pdf");
const ctx = canvas.getContext("2d");
loadImage(`URLTOImage`).then(image => {
ctx.drawImage(image, 0, 0, 2480, 3508);
const buffer = canvas.toBuffer("application/pdf");
})
You can't.你不能。 Plain and simple.
干净利落。
You're attempting to convert binary filetypes, which can't realistically be achieved in the browser, and for good reason.您正在尝试转换二进制文件类型,这在浏览器中实际上无法实现,这是有充分理由的。
This is a server-side operation, and therefore needs to be performed on a server in some way-shape-or-form.这是服务器端操作,因此需要以某种方式在服务器上执行。
@IVOGELOV has already provided a solution for you, which is available on github for free: @IVOGELOV 已经为您提供了一个解决方案,可以在 github 免费获得:
https://github.com/alvarcarto/url-to-pdf-api https://github.com/alvarcarto/url-to-pdf-api
There's even a button at the very top of the README, that takes you directly to Heroku to deploy it.自述文件的最顶部甚至还有一个按钮,可直接将您带到Heroku进行部署。
Just click the link in the following image:只需单击下图中的链接:
You can get it up and running in minutes, for free.您可以在几分钟内免费启动并运行它。
luckily i got the solution by myself.幸运的是我自己得到了解决方案。 i used pdfmake dependency to make it work, here goes the code:
我使用 pdfmake 依赖项使其工作,代码如下:
import pdfMake from 'pdfmake/build/pdfmake' import { debounce } from 'lodash'从 'pdfmake/build/pdfmake' 导入 pdfMake 从 'lodash' 导入 { debounce }
docDefinition() {
return {
content: [
{
image: 'base64code of Image'
width: 550,
height: 820,
},
],
pageSize: 'A4',
pageOrientation: 'portrait'
}
},
async makePDFFun() {
this.generatePDF = await debounce(() => {
pdfMake.createPdf(this.docDefinition()).getDataUrl(dataUrl => {
this.pdfDataUrl = dataUrl
saveAs(this.pdfDataUrl)
})
}, 0)
const rar = await this.generatePDF() }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.