Jspdf: Unable to generate pdf from dataUrl

Created on 8 Dec 2020  路  3Comments  路  Source: MrRio/jsPDF

I'm pretty much new into this library but i could not find a method which would allow me to generate a pdf from a pdf dataUrl.
Sample dataUrl: data:application/pdf;base64,JVBERi0xLjUNCiXx+ff2DQolMy4zDQo2IDAgb2JqDQo8PA0KL0JpdHNQZXJDb21wb25lbnQgOCANCi9Db2xvclNwYWNlIC9EZXZpY2VSR0IgDQovRmlsdGVyIFsgL0ZsYXRlRGVjb2RlIC9EQ1REZWNvZGUgXQ0KDQovSGVpZ2h0IDIyMDAgDQovTGVuZ3RoIDcgMCBSIA0KL05hbWUgL0ltYWdlUGFydF8wIA0KL1N1YnR5cGUgL0ltYWdlIA0

Here is a brief explanation of my scenario:

  1. I upload pdf which are being read by JS File reader and corresponding dataUrl of the file reader is being passed to the node backend.
  2. I need to convert this uploaded pdf to png image.

From whatever i found on docs, i believe that i could something like:

   const { jsPDF } = require("jspdf");

    const doc = new jsPDF();

     Once i have converted the dataUrl into an image i can use **add page** method to add it to the pdf 

     doc.save()

Secondly, it would be off great help if you could give some clear examples along with the api methods in docs.

Question

All 3 comments

Sorry, but from your description I don't fully understand what you want to achieve. Do you want to convert a PDF file to a PNG image or to you want to convert a PNG image to a PDF? Or do you want to read a PDF file, add a PNG image and save it again? Please clarify.

Please also note that jsPDF does not support parsing or modifying existing PDF files.

@HackbrettXXX Thanks a lot for the swift response 馃檹 . I apologize for the lack of clarity in my previous comment.

So here is what i want to do:

  1. In the frontend of my app, i allow the users to upload a pdf file. I read that pdf file using the native js FileReader like :
    const reader = new FileReader(); reader.readAsDataURL(file);
  2. This would give me the uploaded pdf data url like the one below
    Sample dataUrl: data:application/pdf;base64,JVBERi0xLjUNCiXx+ff2DQolMy4zDQo2IDAgb2JqDQo8PA0KL0JpdHNQZXJDb21wb25lbnQgOCANCi9Db2xvclNwYWNlIC9EZXZpY2VSR0IgDQovRmlsdGVyIFsgL0ZsYXRlRGVjb2RlIC9EQ1REZWNvZGUgXQ0KDQovSGVpZ2h0IDIyMDAgDQovTGVuZ3RoIDcgMCBSIA0KL05hbWUgL0ltYWdlUGFydF8wIA0KL1N1YnR5cGUgL0ltYWdlIA0.
  1. I then pass this to the node js backend where i want to convert this pdf into png image.

Please let me know whether there is a way to achieve this with jsPDF

Unfortunately, this is not possible with jsPDF. JsPDF cannot parse existing files and cannot convert a PDF file to an image.

Was this page helpful?
0 / 5 - 0 ratings