Categories
JavaScript Answers

How to set image to fit width of the page using jsPDF and JavaScript?

Spread the love

Sometimes, we want to set image to fit width of the page using jsPDF and JavaScript.

In this article, we’ll look at how to set image to fit width of the page using jsPDF and JavaScript.

How to set image to fit width of the page using jsPDF and JavaScript?

To set image to fit width of the page using jsPDF and JavaScript, we call the addImage method.

For instance, we write

const doc = new jsPDF("p", "mm", "a4");

const width = doc.internal.pageSize.getWidth();
const height = doc.internal.pageSize.getHeight();
const imgData = "data:image/jpeg;base64,/9j/4AAQSkZJ......";

doc.addImage(imgData, "JPEG", 0, 0, width, height);

to get the width and height of the page with getWidth and getHeight.

Then we call addImage with imgData, width and height to add the image with the given width and height.

0 and 0 are the x and y coordinates of the top left corner of the image.

Conclusion

To set image to fit width of the page using jsPDF and JavaScript, we call the addImage method.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.