How to Add an Image From URL to PDF Using jsPDF

Default Featured Image

In this tutorial, you will learn how to add an image from a URL to a PDF document using the jsPDF library. Basically, instead of using jsPDF.js library, we will use the jsPDF.debug.js because it includes all the modules which we need.

The complete source code to add images from URL to PDF using JavaScript library jsPDF is given below.

var pdf = new jsPDF(); var img = new Image; img.onload = function() < pdf.addImage(this, 10, 10); pdf.save("CTStest.pdf"); >; img.crossOrigin = ""; img.src = 'D:/work/TiffImages/png/895153.0000.png';
let logo = null; getDataUri(imgUrl, function(dataUri) < logo = dataUri; console.log("logo=" + logo); >); function getDataUri(url, cb) < var image = new Image(); image.setAttribute('crossOrigin', 'anonymous'); //getting images from external domain image.onload = function () < var canvas = document.createElement('canvas'); canvas.width = this.naturalWidth; canvas.height = this.naturalHeight; //next three lines for white background in case png has a transparent background var ctx = canvas.getContext('2d'); ctx.fillStyle = '#fff'; /// set white fill style ctx.fillRect(0, 0, canvas.width, canvas.height); canvas.getContext('2d').drawImage(this, 0, 0); cb(canvas.toDataURL('image/jpeg')); >; image.src = url; >

Now to generate the pdf document use the code below.

var doc = new jsPDF(); let left = 15; let top = 8; const imgWidth = 100; const imgHeight = 100; doc.addImage(logo, 'PNG', left, top, imgWidth, imgHeight); doc.output('dataurlnewwindow'); //opens pdf in new tab

Add an Image From URL to PDF Using jsPDF

index.html

    

Related posts:

  1. Convert JPG/PNG Image to Pixelated & Blurred Image Using Node.js ImageMagick Express.js
  2. Convert JPG/PNG Image to Pixelated & Blurred Image Using ImageMagick Command
  3. Flutter Build PDF Documents Viewer From URL Using Syncfusion
  4. Download PDF File From URL Using jQuery AJAX Method
  5. Python Download PDF From URL Using BeautifulSoup4 and Requests Library
  6. PHP 7 FPDF Example: Add TrueType Fonts inside PDF Document
  7. PHP 7 FPDI Edit Existing PDF Document & Add Text Inside It
  8. PHP 7 TFPDF Add UTF-8 Fonts Inside PDF Document
  9. html2pdf.js Add Page Break in PDF File Using JavaScript
  10. HTML2PDF.js Add Page Numbers at Top in PDF Using JavaScript
  11. JavaScript HTML2PDF.js Add Header/Footer to All PDF Pages
  12. jsPDF Autotable Tutorial: Draw Colorful Rounded Rectangle in a Cell of Table inside PDF Document
  13. PHP 7 jsPDF Html2Canvas Example: Send Generated PDF as Email Attachment to Client Using JavaScript
  14. jsPDF Html2Canvas Project: Export Multiple Google Charts from Webpage to PDF Document in JavaScript
  15. jsPDF Html2Canvas Project: Export HTML With Multiple Graphs Plotted to PDF Document in JavaScript
  16. Angular 13 jsPDF Html2Canvas Project: Export Multiple HTML Div Content in Different PDF Pages Using TypeScript
  17. jsPDF Encode PDF as BLOB Using Base64 Code & Download it
  18. jsPDF Set the Opacity of Text Color inside PDF Document
  19. jsPDF addHTML() Convert HTML Tables to PDF Using JavaScript
  20. jsPDF .fromHTML() Convert HTML Div Paragraphs to PDF in JavaScript