Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Calculator

0

164
Views
JS JQUERY JSPDF How can I download images and input values from a BLOB object as a PDF

I'm able to convert my HTML code to a BLOB object as a PDF.

    var that = this;
    var doc = new jsPDF('p', 'pt', 'a4');
    var htmlString = '<div class="container"> <img src="base64image" alt="Image"> <input value="testing"><p> Paragraph</p> <label class="bold" for="fname">Vehicle</label></div>';

    doc.fromHTML(htmlString, 20, 20, {}, function (bla) {
        doc.save('saveDOC.pdf'); // After downloading PDF i can see image and Label. But i cant see my input values.
    },0);

    // Below code is to convert HTML into Blob as PDF

    var blob = new Blob([doc.output("blob")], {
        type: "application/pdf"
    });

In the above code variable htmlString contains <img/> , <input/>, <label/>. When i try to download the BLOB object as PDF, Im getting blank PDF. But when i remove <img/> , <input/> then im able to view the content in BLOB object PDF.

Below is the code for converting BLOB as PDF..

const downloadFile = (blob, fileName) => {
    const link = document.createElement('a');
    link.href = URL.createObjectURL(blob);
    link.download = fileName;
    document.body.append(link);
    link.click();
    link.remove();
    setTimeout(() => URL.revokeObjectURL(link.href), 7000);
};
downloadFile(new Blob([doc.output("blob")]), "document.pdf");

How can I get images and input values into a downloaded PDF from a BLOB object?

Thank you in advance.

7 months ago · Juan Pablo Isaza
1 answers
Answer question

0

There are 2 things you have to know about jsPdf that might be your issue:

  1. Regarding images. jsPDf try to read the image and put in into the PDF by using JavaScript. Because of content security policy standards, jsPdf cannot add images that exists on other domains. (Unless they permit it). Webpage can embed those images into the page, but JavaScript doesn't have access to the content of those images. It's like an IFRAME. You can add embed another website, but cannot access to it. Think about someone who will embed YouTube, and will be able to fetch your Google Username. It's impossible. Same for images. If you use the data as in the example, maybe there is an issue with out you encoded the image (You didn't provide your input)

  2. Regarding input fields. jsPdf doesn't read the text in the inputs. It's not part of the HTML, it's just the corrent state (even if you put it in the value attribute). Also jsPdf doesn't read the color of link (If you visited or not..., it just the state). So in you case, I suggest that you will transform all input field to simple text with the same style (you can use DIV elements...)

7 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2023 PeakU Inc. All Rights Reserved.