简体   繁体   中英

jsPDF add image issue when opening with adobe Acrobat reader

On click of a button, I am creating a PDF, adding image and text to it, and then saving it to disk.

var pdf = new jsPDF('l', 'pt', [1600, 1000]);

var img = new Image;
img.src = 'http://localhost:29576/Images/logo.png';
img.src = logoUrl;

pdf.addImage(img, 10, 10)
pdf.save();

Now everything works fine and I am able to view this from Chrome. But the moment , I open this from Acrobat Reader, I get an error:

There was an error processing a page. There was a problem reading this document (110)

在此处输入图片说明

You have two mistakes:

  • you write img.src = ... two times. In second case you overwrites the first value;
  • you have to wait until the image was loaded. This means you have to add + save the image only after event onload .

The solution

var pdf = new jsPDF('l', 'pt', [1600, 1000]),
    img = new Image,
    logoUrl = 'http://localhost:29576/Images/logo.png';

img.onload = function()
{
    pdf.addImage(img, 10, 10);
    pdf.save('example.pdf');
};
img.src = logoUrl;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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