0

I am trying to convert the html file to pdf using jsPDF library. But in the code I implemented, the whole image is not converting into pdf. Only half of it is visible. Could someone please tell me where am I wrong?

var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
context.beginPath();
context.moveTo(0,0);
context.lineTo(200,100);
context.closePath();
context.strokeStyle = '#0000ff';
context.stroke();

download.addEventListener("click", function() {
  // only jpeg is supported by jsPDF
 var imgData = canvas.toDataURL("image/jpeg",1.0);              
var pdf = new jsPDF();
 pdf.addImage(imgData, 'JPEG', 0, 0);

var download = document.getElementById('download');
  pdf.save("download.pdf");
});
<script src="https://parall.ax/parallax/js/jspdf.js"></script>
<canvas id="myCanvas" width="200" height="100" style="border:solid #0000ff;">
</canvas>
<button id="download">download</button>

1 Answer 1

1

I recently did some research on converting html to pdfs. I looked at jsPDF, but it seemed better suited for embedding PDF documents into html pages.

If you're looking to export a html page to PDF you may want to consider https://github.com/fraserxu/electron-pdf, which is what I ultimately decided to use. Electron generates output consistent with the 'print to pdf' feature in Chrome.

Note: this is a server-side rendering solution. If you have to do it client-side then it is not the right solution.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.