Use two different width controls: set jsPDF’s width to the PDF-space width you want, and set windowWidth to the CSS-pixel width at which the HTML should be laid out. Keep raster resolution separate with html2canvas.scale. A reliable export therefore measures the PDF page, measures the source element, and passes those values explicitly.
The working pattern
jsPDF’s html() method renders an element through html2canvas and places the result in the PDF. Its width option is measured in jsPDF document units (points, millimetres, or the unit selected when creating the document). The rendered element is scaled until it fits that width. It is not a CSS width and it is not an html2canvas option.
windowWidth is the CSS-pixel width used for the rendering container. In jsPDF’s HTML API it affects container sizing, but it does not change CSS media queries. It only has an effect when width is also supplied. The following example exports an A4 page with 24-point margins:
const element = document.querySelector('#export');
const pdf = new jsPDF({
unit: 'pt',
format: 'a4',
orientation: 'portrait'
});
const pageWidth = pdf.internal.pageSize.getWidth();
const cssWidth = element.scrollWidth;
const cssHeight = element.scrollHeight;
pdf.html(element, {
margin: 24,
width: pageWidth - 48,
windowWidth: cssWidth,
autoPaging: 'text',
html2canvas: {
scale: 1,
windowWidth: cssWidth,
windowHeight: cssHeight,
useCORS: true
},
callback: (doc) => doc.save('export.pdf')
});
Here, width controls the final PDF placement. The two html2canvas dimensions describe the source canvas, while scale controls its pixel density. If you omit width, changing windowWidth will not produce the intended fit-to-page behavior.
#1 Best Overall
- EDIT text, images & designs in PDF documents. ORGANIZE PDFs. Convert PDFs to Word, Excel & ePub.
- READ and Comment PDFs – Intuitive reading modes & document commenting and mark up.
- CREATE, COMBINE, SCAN and COMPRESS PDFs
- FILL forms & Digitally Sign PDFs. PROTECT and Encrypt PDFs
- LIFETIME License for 1 Windows PC or Laptop. 5GB MobiDrive Cloud Storage Included.
What each width-related option actually does
| Option | Where it belongs | Unit | Purpose | Common mistake |
|---|---|---|---|---|
width |
jsPDF html() |
PDF document units | Target width in the PDF; the rendered element is scaled to fit. | Putting it inside html2canvas, where it does not control PDF placement. |
windowWidth |
jsPDF html() |
CSS pixels | Sets the width of the rendering container used by jsPDF’s HTML renderer. | Expecting it to alter CSS media queries or work without width. |
html2canvas.scale |
html2canvas options |
Multiplier | Controls raster resolution. The default is the browser’s window.devicePixelRatio. |
Using it as a layout-width setting; it changes pixels and can increase memory use. |
html2canvas.windowWidth |
html2canvas options |
CSS pixels | Viewport width used by html2canvas while painting. | Setting only the jsPDF value and leaving the canvas viewport inconsistent. |
html2canvas.windowHeight |
html2canvas options |
CSS pixels | Viewport height for the canvas; matching the element’s scroll height helps prevent clipping. | Leaving a short viewport for a long, scrollable export. |
Use a PDF unit that matches your measurements. With unit: 'pt', an A4 page is about 595.28 points wide; calculating it through pdf.internal.pageSize.getWidth() avoids hard-coding a paper-size value. Subtract both margins before assigning width.
Choosing the CSS layout width
Preserve the desktop layout
For a dashboard or fixed-width report, set windowWidth to the element’s scrollWidth. This lets the renderer see the full horizontal layout instead of the narrow browser viewport. The resulting content is then reduced to the PDF’s available width by jsPDF’s width option.
Force a known responsive breakpoint
If your document intentionally changes at breakpoints, provide an explicit CSS width such as 1024 pixels and style the export root for that width. Remember that jsPDF’s windowWidth controls container size; it does not itself change media-query evaluation. Test the same breakpoint in a real browser before exporting.
Prevent accidental horizontal overflow
Inspect scrollWidth and clientWidth. A larger scrollWidth may indicate a table, preformatted code, or absolutely positioned element that cannot shrink. Decide whether to wrap, hide, or intentionally scale that content. Shrinking an already wide canvas with a very small PDF width can make text unreadable even though the export technically fits.
Free tools Windows power users keep installed
One-click scans. No signup required.
Pagination and page-break behavior
Set autoPaging according to the document’s structure:
falsedisables automatic page insertion.trueor'slice'slices content at page boundaries. Shapes and text chunks can be cut.'text'attempts to avoid splitting text across pages and is generally best for mostly single-column prose.
Text mode is not a complete CSS pagination engine. Keep headings with the content they introduce using print-oriented CSS where possible, and avoid relying on complex floats, transforms, or overlapping positioned elements. A long table may still split in an undesirable place; splitting it into logical sections or exporting a dedicated print layout is more predictable.
Resolution, sharpness, and file size
The HTML output is rasterized by html2canvas before jsPDF places it. Increasing html2canvas.scale produces more pixels and can improve small text and high-density display output, but it also increases memory use, processing time, and PDF size. The default scale is window.devicePixelRatio; start with 1 for a controlled, compact file and raise it only when inspection shows insufficient detail.
Do not compensate for a wrong layout width with a larger scale. First correct width, windowWidth, and the element’s dimensions. Then tune scale for visual quality. There is no universal performance number: document length, images, fonts, browser, scale, and available memory all affect runtime.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Rank #2
- Edit PDFs with Ease. Modify text, images, and layouts directly within your PDF documents.
- Convert & Organize. Export PDFs to Word, Excel, or ePub, and organize files with ease.
- Read & Annotate. Enjoy intuitive reading modes and powerful tools to comment, highlight, and mark up PDFs.
- Create & Manage PDFs. Create new PDFs, combine multiple files, scan documents, and compress for easy sharing.
- Fill & Sign Forms. Complete forms and digitally sign documents with secure e-signature tools.
Images, fonts, and CSS fidelity
Cross-origin images
Images from another origin can taint the canvas and disappear from the PDF. useCORS: true works only when the image server sends an appropriate Access-Control-Allow-Origin response. If you control neither origin nor headers, serve the image through a same-origin proxy or embed it as data with the required permissions.
Fonts and late-loading assets
Wait until web fonts and images have loaded before calling html(). A practical approach is to run the export from a user action after the page is visible, and to await document.fonts.ready when your browser support target provides it. Missing fonts change line wrapping, which in turn changes page count.
CSS support is selective
html2canvas implements CSS properties individually rather than running a browser’s full painting engine. It therefore does not provide complete CSS support. Verify gradients, filters, blend modes, transforms, pseudo-elements, sticky positioning, and complex grid or flex layouts in the browsers you support. A dedicated print stylesheet with simpler colors and flow often produces a more stable result.
Diagnosing common failures
| Symptom | Likely cause | Fix |
|---|---|---|
Changing windowWidth appears to do nothing. |
jsPDF’s width was omitted. |
Set both: a PDF-space width and a CSS-pixel windowWidth. |
| Everything is tiny or blurry. | Source layout is too wide, or scale is inappropriate. | Choose a deliberate CSS width, fit it to the available PDF width, then adjust scale separately. |
| Right-hand content is clipped. | Canvas viewport does not include the element’s full scroll dimensions. | Pass matching windowWidth: element.scrollWidth and windowHeight: element.scrollHeight; inspect overflow. |
| Output is blank or only partly rendered. | Canvas-size limits, excessive dimensions, or failed resources. | Reduce the export into sections, lower scale, shorten a single canvas, and check browser console errors. Canvas limits vary by browser. |
| Images are missing. | Cross-origin canvas restrictions or unavailable URLs. | Use CORS-enabled image responses with useCORS: true, or a same-origin proxy. |
| Text breaks differently from the page. | Different container width, fonts not ready, or unsupported CSS. | Set the intended windowWidth, wait for fonts, and simplify unsupported styling. |
Node.js throws errors about window or document. |
html2canvas requires browser APIs. | Run the export in a browser, or use a browser-driving server tool such as Puppeteer or Playwright. |
A reusable export function
async function exportElementToPdf(selector, filename = 'export.pdf') {
const element = document.querySelector(selector);
if (!element) throw new Error(`No element matches ${selector}`);
if (document.fonts?.ready) await document.fonts.ready;
const pdf = new jsPDF({ unit: 'pt', format: 'a4', orientation: 'portrait' });
const margin = 24;
const width = pdf.internal.pageSize.getWidth() - margin * 2;
const windowWidth = element.scrollWidth;
const windowHeight = element.scrollHeight;
return new Promise((resolve, reject) => {
try {
pdf.html(element, {
margin,
width,
windowWidth,
autoPaging: 'text',
html2canvas: {
scale: 1,
windowWidth,
windowHeight,
useCORS: true
},
callback: doc => {
doc.save(filename);
resolve();
}
});
} catch (error) {
reject(error);
}
});
}
exportElementToPdf('#export');
Keep the export root free of transient controls such as “Download” buttons. If the page contains a scroll container, target its inner content rather than the clipped outer shell.
Quick wins for a faster PC:
Repair Windows errors before they cause bigger problemsFix Now →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Browser versus server generation
html2canvas depends on window, document, computed styles, and other browser APIs, so it cannot run directly in Node.js. For server-side generation, run a real browser with Puppeteer or Playwright, set the page viewport and wait for fonts and network resources, then use the browser’s PDF facilities or execute the client-side export inside that page. This adds browser lifecycle, sandboxing, authentication, and resource-timeout concerns that do not exist in a simple client-side download.
Or skip the browser setup
For a URL you control or can access, ScreenshotNeo returns a screenshot or PDF through one request. Cookie and consent banners, newsletter popups, and chat widgets are removed before the shot; bot checks, blank pages, timeouts, failed loads, and cache hits are not billed, and the response identifies the result with X-Page-Verdict and X-Billed headers. Its MCP server provides take_screenshot, get_page_info, and capture_pdf tools to Claude, Cursor, and other MCP clients.
curl -G "https://api.screenshotneo.com/v1/shot"
-d access_key=YOUR_API_KEY
--data-urlencode url=https://stripe.com
-o shot.webp
See the ScreenshotNeo API documentation for PDF and rendering options.
import requests
r = requests.get(
"https://api.screenshotneo.com/v1/shot",
params={"access_key": "YOUR_API_KEY", "url": "https://stripe.com"},
timeout=90
)
open("shot.webp", "wb").write(r.content)
const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://stripe.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);
Every plan includes its features. The Free plan provides 1,000 shots per month without a card; paid plans start at $5 for 3,000 shots. Create a free ScreenshotNeo account to try it.
Recommended Free Tools
FAQ
Does width change the HTML element’s CSS width?
No. It is the target width in PDF document units used when jsPDF places the rendered result.
Can I make selectable PDF text with this method?
The html2canvas-based pipeline rasterizes the HTML, so the resulting content is generally an image rather than searchable, selectable text.
Rank #3
- EVERY PDF TOOL UNLOCKED - 30+ tools in one app: edit text and images, convert, merge, split, compress, sign, OCR, redact, watermark, batch process, and more. No feature gates, no upsells, nothing held back.
- PAY ONCE, OWN FOREVER — A one-time purchase, not a subscription. Other apps runs $240/year — Scrivar is yours for life, with free updates included.
- UNLIMITED eSIGN, BUILT IN — Send contracts and forms for signature and track every step. Recipients sign in their browser with no account or app needed. Replace DocuSign and save hundreds a year.
- PC, MAC, AND WEB — Install on any Win 10/11 PC or macOS 11+ Mac (Intel or Apple Silicon), or work in your browser at scrivar.com. Same tools, same account, everywhere you work.
- OCR + FULL OFFICE CONVERSION — Turn scanned documents into searchable, selectable text, and convert PDFs to and from Word, Excel, and PowerPoint with formatting kept intact.
Why does a very long page sometimes fail without an exception?
Browser canvas dimensions have implementation-dependent limits. A canvas that exceeds them can become blank or incomplete even when JavaScript reports no explicit error.
When should I choose autoPaging: 'text'?
Use it for predominantly flowing, single-column text when avoiding mid-word or mid-line cuts matters more than exact reproduction of every visual boundary.
Frequently Asked Questions
Does width change the HTML element’s CSS width?
No. It is the target width in PDF document units used when jsPDF places the rendered result.
Can I make selectable PDF text with this method?
The html2canvas-based pipeline rasterizes the HTML, so the resulting content is generally an image rather than searchable, selectable text.
Why does a very long page sometimes fail without an exception?
Browser canvas dimensions have implementation-dependent limits. A canvas that exceeds them can become blank or incomplete even when JavaScript reports no explicit error.
When should I choose autoPaging: 'text'?
Use it for predominantly flowing, single-column text when avoiding mid-word or mid-line cuts matters more than exact reproduction of every visual boundary.
The Bottom Line
For predictable sizing, give jsPDF a PDF-space width, give the renderer an explicit CSS windowWidth and windowHeight, and tune html2canvas.scale only for resolution. Validate cross-origin assets, fonts, CSS support, and canvas limits before shipping.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

