Preview HTML Like wkhtmltopdf Renders It
In this article
Previewing HTML as it will appear when rendered by wkhtmltopdf helps ensure accurate layout, styling, and content output in final PDFs. Since wkhtmltopdf uses an older WebKit-based engine, it may interpret CSS and HTML differently than modern browsers. By simulating its rendering environment, you can catch formatting issues early—such as missing fonts, broken layouts, or unsupported styles—before generating the actual PDF.
What You'll Need
- A local installation of
wkhtmltopdf(version 0.12.6 recommended for stability) - A web browser (Chrome, Firefox, or Safari)
- A text editor or IDE for editing HTML and CSS
- Basic command-line access (Terminal on macOS/Linux, Command Prompt or PowerShell on Windows)
- A sample HTML file to test
Step-by-Step
- Write or obtain the HTML file you plan to convert. Ensure it includes all external resources (CSS, fonts, images) using absolute or relative paths that will be accessible during rendering.
- Open a terminal or command prompt and navigate to the directory containing your HTML file using
cd /path/to/your/file. - Run
wkhtmltopdf --disable-javascript --no-background false --enable-local-file-access input.html output.pdf. This generates a PDF using wkhtmltopdf's rendering engine. The flags ensure local files load and background colors are preserved. - Use a PDF viewer to open
output.pdfand inspect how the content appears. Pay close attention to font rendering, spacing, margins, and image alignment. - To simulate the browser preview, open the same
input.htmlin a modern browser. Note differences in layout, font size, or CSS support (e.g., flexbox or grid may not work in wkhtmltopdf). - Adjust your HTML and CSS to use only features supported by wkhtmltopdf. Avoid modern CSS like
gapin flex containers,position: sticky, ortransformproperties, which may not render correctly. - Use inline CSS or embed styles directly in a
<style>block within the HTML<head>. External stylesheets may fail to load due to path resolution issues in wkhtmltopdf. - Test fonts by embedding them via
@font-facewith local file paths. Use absolute URLs or ensure the font files are in the same directory as the HTML. - Repeat the
wkhtmltopdfcommand after each change to verify improvements. Use the--margin-top,--margin-bottom, etc., flags to adjust page geometry if needed. - For advanced previewing, run
wkhtmltopdf --web-feature-enable --debug-javascript --javascript-delay 2000 input.html output.pdfto capture dynamically generated content, though this may not fully replicate JavaScript-heavy pages.
Common Mistakes
- Assuming modern CSS will work: wkhtmltopdf uses an outdated WebKit engine and lacks support for many CSS3 features. Avoid flexbox, grid, and modern selectors.
- Using external stylesheets without
--enable-local-file-access: by default, wkhtmltopdf blocks local file access for security, causing missing styles. - Forgetting to embed fonts: if fonts aren’t embedded via
@font-facewith relative paths, fallback fonts may alter the layout. - Overlooking image paths: relative image paths must be correct relative to the HTML file, not the command-line working directory.
- Expecting full JavaScript support: while limited JS is supported, complex interactions or DOM manipulations may not execute as expected.
Tips
- Use simple, table-based layouts for maximum compatibility. Tables render more reliably than div-based layouts.
- Set explicit widths and heights in pixels or percentages to prevent layout shifts.
- Test with minimal HTML first, then gradually add complexity to isolate rendering issues.
- Use
<meta charset="UTF-8">in the HTML head to prevent encoding problems, especially with special characters. - Add
--load-error-handling ignoreto the command to continue rendering even if some assets fail to load. - Use the
--viewport-sizeoption (e.g.,--viewport-size 1024x768) to simulate a specific screen size during rendering.
Troubleshooting
If the output PDF does not match expectations, first verify that all assets (CSS, fonts, images) are accessible and paths are correct. Check the terminal output for warnings or errors—common issues include “Failed to load resource” or “Permission denied.” If styles are missing, ensure --enable-local-file-access is used and that the HTML references stylesheets with relative paths.
If fonts do not appear, confirm the font file is in the correct directory and the @font-face rule uses a relative path. Test by opening the HTML in a browser with network disabled to simulate asset loading issues.
If layout is broken, simplify the HTML to use basic tables and inline styles. Avoid div containers with complex positioning. If JavaScript is required, increase the --javascript-delay to allow time for execution.
On Windows, ensure wkhtmltopdf is added to the system PATH or call it using the full path (e.g., "C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe"). On Linux/macOS, ensure execute permissions are set with chmod +x.
Frequently Asked Questions
Why does my CSS not load in wkhtmltopdf?
External CSS files may fail to load without the --enable-local-file-access flag. Use inline styles or embed CSS in a <style> block to avoid path issues.
Can I preview JavaScript-generated content?
Limited JavaScript is supported. Use --javascript-delay to allow time for execution, but complex frameworks may not render correctly.
How do I fix missing fonts in the PDF?
Embed fonts using @font-face with relative paths to font files in the same directory as the HTML.
Is there a GUI tool to preview wkhtmltopdf output?
No official GUI exists, but you can automate the process using scripts or tools like wkhtmltopdf wrappers in Node.js or Python.
Why does the layout differ between browser and PDF?
wkhtmltopdf uses an older WebKit engine. It lacks support for modern CSS layout features, so use simpler, table-based designs for consistency.
Can I use HTTPS resources in the HTML?
Yes, wkhtmltopdf can load external HTTPS resources, but ensure stable connections and avoid mixed content (HTTP inside HTTPS).