question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`doc.internal.pageSize.getWidth()` doesn't match measured value

See original GitHub issue
function pdf() {
  const { jsPDF } = window.jspdf;
  const doc = new jsPDF("p", "px", "a4");
  const pageWidth = doc.internal.pageSize.getWidth();
  console.log("pageWidth", pageWidth);
  
  doc.save("pagesize.pdf")
}

You can run the above code here: https://jsfiddle.net/6htnzja8/1/

Running the example code and looking in the console jsPDF says the page width is 446.46 pixels. If I take a screenshot of the generated PDF and measure the size it’s actually 793 pixels wide.

I think the value reported by jsPDF is wrong. Is it, or am I misunderstanding something?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11

github_iconTop GitHub Comments

1reaction
iron2414commented, Apr 15, 2021

Is this still working? I am using jsPDF 2.3.1 and trying to put images inside a pdf, but they get blurry. When i try to enable “px_scaling” mentioned here, and https://github.com/MrRio/jsPDF/blob/master/HOTFIX_README.md the code won’t compile. After checking the package, there is no “hotfix” in the code, and the jsPDF constructor only takes “jsPDFOptions”

  export interface jsPDFOptions {
    orientation?: "p" | "portrait" | "l" | "landscape";
    unit?: "pt" | "px" | "in" | "mm" | "cm" | "ex" | "em" | "pc";
    format?: string | number[];
    compress?: boolean;
    precision?: number;
    filters?: string[];
    userUnit?: number;
    encryption?: EncryptionOptions;
  }

Edit: I had to pass it in as a constructor parameter, and surpress the statement with // @ts-ignore. It works now properly.

0reactions
HackbrettXXXcommented, Sep 24, 2020

Yeah, for myself and for future reference, I did the math once again:

96px = 1in (css pixels)
72pt = 1in   (PDF 1.3 specification)

-> 96px = 72pt
-> 1px = 72/96pt

Which means we have to multiply each pixel by 72/96 to get pt. So the scaleFactor of 72/96 is correct and one has to enable the hotfix in order to get the correct scaling for pixels. We should really document that the hotfix is required for the px unit and make this the default with the next major release.

Thanks for bringing that up 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

`doc.internal.pageSize.getWidth()` doesn't match measured ...
Running the example code and looking in the console jsPDF says the page width is 446.46 pixels. If I take a screenshot of...
Read more >
How to set image to fit width of the page using jsPDF?
A better solution is to set the doc width/height using the aspect ratio of your image.
Read more >
How to get the page width and height ... - iText Knowledge Base
Most of them return an object of type Rectangle that has methods such as getWidth() and getHeight() to get the width and the...
Read more >
Export multiple charts to PDF with React and jsPDF
How to export multiple charts into multipage pdf document with React.js ... getWidth(); // if chart do not fit to the page width...
Read more >
DataGrid - Pdf Export - Certain options are not applied
then(() => { const pageSize = doc.internal.pageSize; const pageWidth = pageSize.width ? pageSize.width : pageSize.getWidth(); doc.setFontSize(10); ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found