使用xhtmltopdf转HTML到PDF时遇Declaration group closing '}'未找到错误
The error you're hitting comes down to xhtmltopdf's limited support for modern CSS3 features, specifically the prefixed -webkit-keyframes syntax. The library's CSS parser (built on older rendering engines) can't properly parse this animation declaration, leading to the missing closing } error you saw.
Here are two straightforward fixes to resolve this:
Option 1: Remove the keyframe code entirely
PDFs are static documents—CSS animations like your spinning effect won't render or work in the final output anyway. Just delete the problematic@-webkit-keyframesblock from your CSS. This is the simplest solution and eliminates the parsing error immediately.Option 2: Wrap the animation code in a media query to exclude it from PDF rendering
If you need to keep the animation for web display but want xhtmltopdf to ignore it, use a@media not printwrapper (xhtmltopdf defaults to processing print styles):@media not print { @-webkit-keyframes spin { 0% { -webkit-transform: rotate(0deg); } 100% { -webkit-transform: rotate(360deg); } } }This way, the animation applies to web browsers but gets skipped entirely during PDF conversion.
Why this happens:
xhtmltopdf relies on older rendering technology (ReportLab under the hood) that doesn't support many CSS3 dynamic features like keyframes, transitions, or transforms. Even if you fixed the syntax, the animation wouldn't appear in the PDF—so removing or excluding it is both a fix for the error and a logical choice for static PDF output.
内容的提问来源于stack exchange,提问作者Prashant Jha

