question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Incomplete or corrupt PNG file

See original GitHub issue

I’m trying to add an image to my PDF document but get this error Uncaught Error: Incomplete or corrupt PNG file in this line doc.addImage(imgData, 'PNG', 15, 40, 180, 160); , this happens when I add imgData var to my PDF file but not when i add imgPDF var for test.

Any idea cuz this happens

        var zip = new JSZip();
        var img = zip.folder("documets").folder("products");
        var row = "txtRow";

        var imgData = value['data'];
        var imgPDF = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAQAAAADCAIAAAA7ljmRAAAAGElEQVQIW2P4DwcMDAxAfBvMAhEQMYgcACEHG8ELxtbPAAAAAElFTkSuQmCC';

        var doc = new jsPDF('landscape');

        doc.text(20, 20, 'This is the default font.');
        doc.addImage(imgData, 'PNG', 15, 40, 180, 160);

        var pdf = doc.output();

        imgData = imgData.replace(/^data:image\/(png|jpg);base64,/, '');
        img.file("file.png", imgData, {base64: true});
        img.file("file.pdf", pdf,{binary: true});



    zip.file("products.csv", row);
    var content = zip.generate({type:"blob"});
    //FileSaver.js
    saveAs(content, "csv.zip");

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:21

github_iconTop GitHub Comments

26reactions
hauxoncommented, Jan 13, 2016

This happens because the image has not finished loading when you send it to jsPDF. You can ensure your image has finished loading before adding it to the document like this:

var myImage = new Image();. myImage .src = 'https://www.myserver.com/somepath/myImage.png';. myImage .onload = function(){. doc.addImage(myImage , 'png', 5, 5, 40, 10);. doc.save('myPDF.pdf');. };.

Hrannar

1reaction
AshotNcommented, Oct 25, 2017

I’m getting this issue as well, using Base64 which is fully loaded. It’s not consistent either, some images cause it and some don’t

Read more comments on GitHub >

github_iconTop Results From Across the Web

jsPDF Incomplete or corrupt PNG file - Stack Overflow
This type of error occurs because the image has not finished loading when you send to jsPdf, use onLoad event to check the...
Read more >
[Client-side HTML to PDF] Error : Incomplete or corrupt PNG file
When trying to generate a PDF file, using the Client-side HTML to PDF forge component, I keep recieving the Error message "Incomplete or...
Read more >
[Solved]-JsPDF: Error (Incomplete or corrupt png file)-Reactjs
Coding example for the question JsPDF: Error (Incomplete or corrupt png file)-Reactjs.
Read more >
Corrupt PNG file may cause heap overflow in the BlackBerry ...
Even though the PNG extension has been removed from the list of supported file types, the BlackBerry Attachment Service may automatically detect ...
Read more >
T43098 Blender creates corrupt PNG-files
When I render out a PNG sequence out of Blender and try to import into After Effects or Premiere Pro, I always get...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found