Set Image Size in jsPDF while converting HTML to PDF

85 views Asked by At

I'm using jsPDF to convert HTML to PDF. Text and Images are being converted but there are issues coming with image sizes. I'm using the jsPDF version 1.3.4. I' attaching the following code for reference.

 const tempElement = document.createElement("div");
tempElement.innerHTML = HTMLdataString;

   var pdf = new jsPDF("p", "mm");
pdf.fromHTML(
  tempElement,
  10,
  10,
  {
    putOnlyUsedFonts: true,
    margin: [10, 10, 10, 10],
    autoPaging: "text",
    x: 0,
    y: 0,
    width: 190, //target width in the PDF document
    windowWidth: 675, //window width in CSS pixels
    pageSplit: true,
    image: { type: "jpeg", quality: 0.98, width: 100 },
  },
  function (bla) {
    const pdfBlob = pdf.output("blob");

    // Create URL for the Blob
    const pdfUrl = URL.createObjectURL(pdfBlob);

    // Open the PDF in a new tab
    window.open(pdfUrl, "_blank", "text.pdf");
  }
  //options
);

Further, I'm attaching the data coming from React-Quill which is referenced as HtmlDataString above. HTML Data String Consoled

PS: I don't want to use html2canvas as it captures the document. The issue with capturing data is it attached as image in the pdf and user can't select the data from pdf. If there any method or library that can helps to convert the html to pdf ?, answers will be appreciated.

1

There are 1 answers

0
Mr. Terminix On

I am wondering why you are using older version of jsPDF? The method .fromHTML is deprecated and it is very hard to find good documentation.