javascript - JsPDF: Error (Incomplete or corrupt png file) - Stack Overflow

I am trying to add a logo and I tried adding a picture but printing it will show this error:Inplete or

I am trying to add a logo and I tried adding a picture but printing it will show this error:

Inplete or corrupt PNG file

How do I solve this?

I recreated the problem here: =/src/App.js

  const handlePrint = () => {
    console.log("clicked");
    const doc = new jsPDF();

    var img = new Image();
    img.src = "assets/logo-social.png";
    doc.addImage(img, "png", 10, 78, 12, 15);

    doc.text("Title here", 20, 10);
    doc.text("Sample", 20, 15);

    const columns = ["Data"];
    const rows = [];
    data.map((item) =>
      rows.push([
        item.cartItems.map(
          (item) => `${item.name}: ${item.color} = ${item.quantity}`
        )
      ])
    );
    doc.autoTable(columns, rows);
    doc.save("order.pdf");
  };

I am trying to add a logo and I tried adding a picture but printing it will show this error:

Inplete or corrupt PNG file

How do I solve this?

I recreated the problem here: https://codesandbox.io/s/js-pdf-with-data-printable-from-displayed-data-lup6ir?file=/src/App.js

  const handlePrint = () => {
    console.log("clicked");
    const doc = new jsPDF();

    var img = new Image();
    img.src = "assets/logo-social.png";
    doc.addImage(img, "png", 10, 78, 12, 15);

    doc.text("Title here", 20, 10);
    doc.text("Sample", 20, 15);

    const columns = ["Data"];
    const rows = [];
    data.map((item) =>
      rows.push([
        item.cartItems.map(
          (item) => `${item.name}: ${item.color} = ${item.quantity}`
        )
      ])
    );
    doc.autoTable(columns, rows);
    doc.save("order.pdf");
  };
Share Improve this question asked Apr 20, 2022 at 10:11 JS3JS3 1,8794 gold badges34 silver badges83 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 3

The image is not fully loaded at the moment of its usage in doc.addImage. Wait for the image to be fully loaded and call addImage then.

const img = new Image();
img.src = 'image-url';
img.onload = () => {
  // await for the image to be fully loaded
  doc.addImage(img,'png',...);
  doc.text("Sample", 20, 15);
  //...
  doc.save('order.pdf');
};

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745146241a4613660.html

相关推荐

  • javascript - JsPDF: Error (Incomplete or corrupt png file) - Stack Overflow

    I am trying to add a logo and I tried adding a picture but printing it will show this error:Inplete or

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信