Odoo11中wkhtmltopdf无法识别CSS样式的配置求助
Hey there, let's work through this CSS rendering issue with your Odoo 11 and wkhtmltopdf setup. I've dealt with similar headaches on Mac before, so here are the most actionable fixes to try:
1. Make Sure Odoo Can Find Your wkhtmltopdf Binary
First, double-check that Odoo knows exactly where your wkhtmltopdf is installed—MacOS often has multiple possible paths depending on how you set it up:
- Open your Odoo config file (usually
odoo.conf) and add/update this line (adjust the path to match your installation):wkhtmltopdf = /usr/local/bin/wkhtmltopdf - If you used Homebrew, the path might look like
/usr/local/Caskroom/wkhtmltopdf/0.12.1/bin/wkhtmltopdf(swap the version number for your installed one). - You can also set this via the Odoo web interface: Go to Settings > Technical > Parameters > System Parameters, search for
wkhtmltopdf.path, and paste the correct binary path. Restart Odoo after making this change.
2. Use Odoo's Recommended Patched wkhtmltopdf Version
Odoo 11 specifically recommends wkhtmltopdf 0.12.1 with patched Qt—the standard 0.12.1 or 0.12.4 versions often have rendering inconsistencies with Odoo reports:
- Uninstall your current version first (e.g.,
brew uninstall wkhtmltopdfif you used Homebrew). - Grab the patched MacOS package from Odoo's official archives (look for
wkhtmltopdf-0.12.1_osx-cocoa-x86-64.pkg) and install it. This version fixes many CSS-related bugs that plague standard builds.
3. Validate Your Report's CSS References
If your custom report isn't pulling in CSS correctly:
- Use Odoo's static file helper instead of absolute paths. For example, in your QWeb template:
<link rel="stylesheet" href="/your_module_name/static/src/css/report_style.css"/> - Avoid local file paths like
/Users/you/odoo/addons/...—wkhtmltopdf might not have access to these. - Test the HTML preview first: Open the report, click the dropdown next to "Print", select "HTML Preview". If CSS doesn't show up here either, the issue is with your reference, not wkhtmltopdf.
4. Tweak wkhtmltopdf Rendering Options
Add these settings to your odoo.conf to fix common layout breaks:
# Disable smart shrinking (often messes up CSS layouts) wkhtmltopdf_options = --disable-smart-shrinking # Set a consistent DPI for better rendering wkhtmltopdf_options = --dpi 96
Combine multiple options with spaces if needed:
wkhtmltopdf_options = --disable-smart-shrinking --dpi 96 --enable-javascript
5. Flush All Caches
Old cached CSS files could be overriding your changes:
- In Odoo: Go to Settings > Technical > User Interface > Clear Cache to flush Odoo's static file cache.
- In your browser: Hard-refresh the HTML preview page (Cmd + Shift + R on Mac) to clear browser cache.
6. Test wkhtmltopdf Directly via Command Line
To rule out Odoo-specific issues, test wkhtmltopdf with a simple local HTML file:
- Create
test.html:<html> <head> <style> .highlight { color: #ff0000; font-size: 24px; } </style> </head> <body> <p class="highlight">This text should be red and large</p> </body> </html> - Run this command:
wkhtmltopdf test.html test.pdf
If the PDF doesn't show the correct styling, your wkhtmltopdf installation is the problem—reinstall the patched version. If it does, the issue lies in Odoo's report configuration.
内容的提问来源于stack exchange,提问作者Deb

