Jspdf: Unable to add base64 png images to JSPDF Javascript

Created on 22 Oct 2017  路  4Comments  路  Source: MrRio/jsPDF

function save_pdf() {
 var doc = new jsPDF();  
 var imgSampleData ='data:image/png;base64,/9j/4AAQSkZJRgABAAEA8ADwAAD/2w...';
 doc.addImage(btoa(imgSampleData), 'PNG', 15, 40, 175, 75);
 doc.save('MCR.pdf');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.5/jspdf.debug.js"></script>

image
https://drive.google.com/file/d/0BzRwV-8m4H86VHpFS1VFVFU4OWs/view

I have attached entire base64 String of whole image. This Base64 works fine and able to see images if I use any online conversion tool.

Attachement base64 png string
Please use the attached string in jspdf code imagedata and Also please copy and paste the Base64 string to browser which seems to work fine.

What could be the reason why JSPDF unable to parse this type of base64 strings and other online PNG base64 to image converters seems to parse the String Successfully?

Most helpful comment

Because of your problem I wrote change so that images are now recognized by the header. I hope it gets accepted, and then problems like yours should be not happening again ;).

All 4 comments

PNG in jspdf in the current version is kind of buggy. E.g. it is missing CMYK recognition support and Interlace-Mode.
See #1455 and #1457
Maybe if you apply those changes it will work, but I doubt it.

To check your image you should publish the file to everyone.

hi thank you for the fast response!
Please check the file below @arasabbasi
https://drive.google.com/file/d/0BzRwV-8m4H86VHpFS1VFVFU4OWs/view?usp=sharing
copy and paste the url in new browsers window or tab

The file is clearly a jpg and not a png.
image

  • change the "data:image/png;base64,/9..." to "data:image/jpeg;base64,/9..."
  • Dont use btoa (not necessary you can directly give the base64 string)
  • set second parameter of addImage to 'JPEG'
  • enjoy result
  • close this issue, because there is no error with the code.

Because of your problem I wrote change so that images are now recognized by the header. I hope it gets accepted, and then problems like yours should be not happening again ;).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sajesh1985 picture sajesh1985  路  5Comments

mellisa0109 picture mellisa0109  路  3Comments

glaier picture glaier  路  3Comments

BarathArivazhagan picture BarathArivazhagan  路  4Comments

Pinank picture Pinank  路  3Comments