Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

333
Views
How to send pdf generated from screen shot in JavaScript

Objective - Take a screenshot of the page, then make pdf of that and send that pdf to the spring boot backend.

I have implemented the functionality to take the screenshot and convert it into pdf using html2canvas and pdfmake JavaScript libraries.

Issue - I don't know how to send the generated pdf to the backend. I got to know about base64 encode but I am still confused.

Code -

function takeSS() {
    html2canvas(document.body, {
        onrendered: function(canvas) {
            var data = canvas.toDataURL();
            var docDefinition = {
                content: [{
                    image: data,
                    width: 900,
                }]
            };
            pdfMake.createPdf(docDefinition).download("test.pdf"); // to download
        }
    });
}

The above code is taking the screenshot of the page converting it to pdf and downloading it. Instead of downloading it, how can I send this pdf to backend? Do I need to base64 encode canvas.toDataURL()?

Edit Changed my code - jsFiddle

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

According to the docs of pdfmake you're able to get the generated PDF as a Blob with the getBlob method. The Blob will be a binary representation of your PDF which you can send to your server.

Blobs need to be wrapped inside a FormData object before they can be sent.

Edit: Switch to the latest version (1.4.1) of html2canvas. The older version didn't work properly. The latest versions uses Promises which allows us to use async functions and, in my opinion, makes your code more readable.

The beta version of pdfmake uses Promises instead of a callback function, but the latest stable version (0.2.5) does not. But we can solve this by wrapping the callback with a Promise to still be able to use the async / await syntax.

async function takeSS() {
  const canvas = await html2canvas(document.body);
  const data = canvas.toDataURL();
  const docDefinition = {
    content: [{
      image: data,
      width: 900,
    }]
  };
  
  const pdfDocGenerator = pdfMake.createPdf(docDefinition);
  const blob = await new Promise(resolve => {
    pdfDocGenerator.getBlob(resolve);
  });
  
  const formData = new FormData();
  formData.append('pdf', blob, 'test.pdf');
  
  try {
    const response = await fetch('myapi-url', {
      method: 'POST',
      body: formData
    });
    
    if (!response.ok) {
      throw new Error('POST request failed');
    }

    const message = await response.text();
    console.log(message);
  } catch (error) {
    console.error(error);
  }
}
about 4 years 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 vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!