Posts

Showing posts from October, 2020

How to print image to PDF using pdfmake

Recently I was required to print web page contents to PDF file and the web page contains JPEG and SVG format images. I used JavaScript library PDFmake (http://pdfmake.org/) to print webpage to PDF in front-end.   To print image to PDF,  if it's JPEG or PNG format then you need to get its Data URL, and it's its SVG then just get its text content. I used below functions to get image values.   Function to get DataURL of image  const  readAsDataURL =  async  (url) => {      const  response =  await  fetch(url);      const  blob =  await  response.blob();      return   new   Promise ((resolve, reject) => {          let  fileReader =  new   FileReader ();         fileReader.onload =  function  () {              return  resolve(fileReader.result);         };         fileReader.readAsDataURL(blob);     }); };     Function to get SVG text content. const  readAsText =  async  (url) => {      const  response =  await  fetch(url);      const  text =  await  response.text();      r