Quick wins for a faster PC:
Clear out junk files and repair common Windows errorsFree Scan →Scan for outdated or missing drivers - takes under a minuteDriver Scan →If a Python pdfkit table of contents (TOC) fits on its first page but later pages start at the top edge, the usual cause is the generated TOC stylesheet—not the document’s normal page margins. wkhtmltopdf builds the TOC from your HTML headings, transforms its outline XML with XSLT, and applies layout rules that may not add spacing to every overflow page. Inspect the generated outline and default XSL, then pass a customized XSL file through the toc argument.
Why the TOC overflows or loses its margin
wkhtmltopdf creates a separate TOC object from the h1–h6 elements in the input HTML. It first builds an outline, then transforms that outline into TOC HTML with XSLT. The resulting TOC is paginated independently from your document body. Consequently, increasing margin-top in the normal page options may change the document pages without fixing spacing on TOC continuation pages.
A common symptom is a correctly spaced first TOC page followed by pages whose first entry touches the top edge or collides with a repeated header. The practical fix is to add explicit spacing and break rules to a custom TOC stylesheet. The exact selectors depend on the wkhtmltopdf build, so inspect its generated default stylesheet instead of copying selectors from an unrelated example.
Inspect the outline and default stylesheet first
Use the same wkhtmltopdf executable that pdfkit will call. These commands reveal whether the problem is excessive heading depth, unexpected headings, or stylesheet layout:
#1 Best Overall
- Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
- Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
- Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
- Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
- 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.
wkhtmltopdf --dump-outline toc.xml document.html /tmp/preview.pdf
wkhtmltopdf --dump-default-toc-xsl > default-toc.xsl
toc.xml shows the heading tree and associated page numbers. Look for headings that should not be in the TOC, such as layout-only labels or hidden template headings. Open default-toc.xsl and identify the wrapper and item markup emitted by your build. Keep those selectors when you customize spacing; different builds can emit different markup.
The default stylesheet also contains the behavior for links, dotted leaders, indentation and font scaling. A fully custom stylesheet does not automatically retain those defaults, so preserve any features you still need.
Build a custom TOC XSL with page-safe spacing
Start with the dumped file
Copy default-toc.xsl to a project file such as toc.xsl. Do not rewrite the outline transformation from scratch. Preserve the templates that generate item links and page-number fields, then add CSS or HTML attributes to the generated TOC wrapper and entries.
Add spacing to every TOC page
For builds whose generated markup uses classes such as .toc-page and .toc-entry, a starting rule is:
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Scan for outdated or missing drivers - takes under a minute3Repair Windows errors before they cause bigger problemsRank #2
- 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
- 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
- Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
- 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
- What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.
<style>
.toc-page {
padding-top: 20mm;
}
.toc-entry {
break-inside: avoid;
page-break-inside: avoid;
}
</style>
Insert equivalent rules into the XSL output or its embedded stylesheet, adapting the selectors to the markup you observed. The top padding must belong to the repeating TOC container (or to each generated page wrapper), not only to the first document page. Avoid placing broad rules on body if the XSL output is combined with other objects.
Prevent individual entries from splitting
Long titles can split across pages even when the top margin is correct. Apply both modern and wkhtmltopdf-compatible declarations to each item:
.toc-entry {
break-inside: avoid;
page-break-inside: avoid;
}
If a single entry is taller than the available page area, the renderer may still move or split it; shorten the heading or reduce TOC text size in that case.
Pass the XSL through Python pdfkit
pdfkit keeps TOC options separate from ordinary page options. Put xsl-style-sheet inside the toc dictionary, not only in options:
Rank #3
- Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
- Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
- Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
- Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
- What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.
import pdfkit
options = {
"page-size": "A4",
"margin-top": "20mm",
"margin-right": "15mm",
"margin-bottom": "20mm",
"margin-left": "15mm",
"encoding": "UTF-8",
}
toc = {
"xsl-style-sheet": "toc.xsl",
}
pdfkit.from_file(
"document.html",
"output.pdf",
options=options,
toc=toc,
)
The page margins define the PDF page box for rendered objects. The custom XSL controls TOC-specific spacing, item breaks and presentation. Keeping those responsibilities separate makes later adjustments predictable.
Control TOC size and appearance
Remove accidental outline entries
Every heading included in the outline adds a TOC item. Use semantic headings for document structure and replace decorative text with ordinary elements styled as needed. If your workflow supports it, bound depth with wkhtmltopdf’s --outline-depth setting. Page-object inclusion can also be controlled with --exclude-from-outline and --include-in-outline.
Use TOC-specific layout settings
wkhtmltopdf exposes settings for indentation by level, font scaling, page offsets, page counting and margins. Use indentation and font scale to reduce width or height pressure, but do not expect those settings to create a top margin on overflow pages when the stylesheet lacks one. A custom stylesheet remains the place for explicit padding and page-break behavior.
Keep leaders and links when customizing
The built-in XSL may provide dotted leaders, clickable links, level indentation and a default text-size factor. A custom XSL does not inherit those behaviors automatically. Copy the relevant templates and fields from the dumped stylesheet, then add your spacing rules so the TOC remains navigable.
Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallRank #4
- Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
- Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
- Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
- Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
- Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft
Cover pages, page numbers and offsets
A cover changes the object order and can make apparently incorrect TOC numbers look like a layout bug. In pdfkit, provide the cover as a separate cover argument. Set cover_first=True when the cover must precede the TOC:
pdfkit.from_file(
"document.html",
"output.pdf",
options=options,
toc=toc,
cover="cover.html",
cover_first=True,
)
If headers, footers and TOC entries are consistently shifted by the same amount, check the global page offset (pageOffset in wkhtmltopdf settings). An offset changes displayed numbering; it does not add physical whitespace. Recheck numbers after adding a cover or changing the offset.
Complete diagnostic workflow
- Verify the executable. Ask pdfkit for its configuration and ensure it points to the intended wkhtmltopdf binary.
- Dump the outline. Confirm the heading hierarchy, depth and page numbers in
toc.xml. - Dump the default XSL. Save the output and locate the generated wrapper and item selectors.
- Copy, do not replace, the templates. Preserve links, leaders, indentation and page-number fields.
- Add scoped spacing rules. Put top padding on the generated TOC page/container and prevent item splitting.
- Pass the file in
toc. Usetoc={"xsl-style-sheet": "toc.xsl"}. - Render with diagnostics. Use pdfkit’s
verbose=Truewhile investigating command-line or asset-loading failures. - Compare at least two TOC pages. Confirm that the first and every continuation page have the intended top spacing.
- Validate in deployment. Fonts, HTML, operating system and wkhtmltopdf build can change pagination; test with the exact production environment.
Troubleshooting common failures
| Symptom | Likely cause | Fix |
|---|---|---|
| Custom XSL has no effect | The stylesheet was placed in normal page options or the path is wrong. | Put "xsl-style-sheet": "toc.xsl" inside toc; use an absolute path while testing. |
| Only later pages lose the margin | Spacing applies only to the first wrapper or body margin. | Apply padding/margin to the repeating TOC container generated by your dumped XSL. |
| TOC is unexpectedly huge | Decorative or deeply nested headings are included. | Change non-structural headings to ordinary elements and limit outline depth. |
| Entries collide or split | Long titles are allowed to break inside an item. | Add break-inside: avoid and page-break-inside: avoid; reduce text size or shorten headings if one item cannot fit. |
| Page numbers are shifted | Cover ordering or page offset changed the numbering model. | Check cover_first, cover argument order and pageOffset; regenerate the TOC. |
Python raises OSError |
pdfkit cannot find the wkhtmltopdf executable. | Install wkhtmltopdf or pass its explicit path through pdfkit.configuration(wkhtmltopdf=...). |
| Assets or fonts fail during rendering | Command-line or resource-loading issue hidden by quiet mode. | Render with verbose=True, inspect the emitted command and verify file paths, permissions and network access. |
Performance, reliability and maintenance
TOC generation adds an outline and XSL transformation before final pagination. The most reliable workflow is deterministic: pin the wkhtmltopdf build used in development and deployment, keep the dumped XSL beside the application, and retain a representative multi-page PDF as a regression fixture. Re-run the fixture after changing headings, fonts, margins, cover order or wkhtmltopdf.
Do not assume a selector copied from an online snippet will work across builds. The generated markup, font metrics and page-breaking behavior can differ. Treat the dumped XSL as the contract for your executable, and inspect the final PDF rather than relying only on a successful process exit.
Best Value
- 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
- Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
- Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
- HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
- What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.
Or skip the browser setup
If your project also needs automated website screenshots, ScreenshotNeo provides a one-request API rather than a locally managed browser. It accepts consent banners before capture and removes more than 60 known consent platforms, newsletter popups and chat widgets; bot checks, blank pages, timeouts, failed loads and cache hits are not billed, with the result identified by X-Page-Verdict and X-Billed headers. Its MCP server exposes take_screenshot, get_page_info and capture_pdf to Claude, Cursor and other MCP clients.
For a direct image request, see the ScreenshotNeo API documentation:
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
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 the capture options, PDF output, custom scripts and styles, waiting controls, request blocking, cookies and headers, device presets, caching, signed links, asynchronous jobs, bulk capture and usage access. The free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000. Create a free ScreenshotNeo account.
What to verify before shipping
- The TOC is generated from intentional HTML headings.
- The custom XSL is passed through
toc, not only normal options. - Continuation pages retain the required top spacing.
- Entries do not split unexpectedly.
- Cover order, page offsets and displayed numbers agree.
- The exact production wkhtmltopdf binary, fonts and operating system have been tested.
Frequently Asked Questions
Can I fix this by increasing pdfkit’s global top margin?
Usually not. Global margins affect the page box, while missing spacing on continuation TOC pages is controlled by the generated TOC stylesheet.
Free tools Windows power users keep installed
One-click scans. No signup required.
Should I write a new XSLT transform from scratch?
No. Dump the default TOC XSL, copy it, preserve its templates and fields, and make narrowly scoped spacing changes.
Why did adding a cover change my TOC numbers?
A cover is a separate document object. Its position and any page offset alter the numbering model, so regenerate and verify the TOC after changing either.
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.

