IronPDF转换带textarea的HTML为可填写PDF时CSS样式不生效的问题求助
IronPDF转换带textarea的HTML为可填写PDF时CSS样式不生效的问题求助
我现在正尝试用IronPDF(2024.8.1.3版本)和Python 3.12.6把一个HTML表单转换成可填写的PDF。HTML在Chrome里渲染完全正常,除了<textarea>标签之外的其他元素在PDF里样式也都没问题(这说明CSS是能被识别的),但IronPDF就是不遵守我给textarea定义的样式——我指定了12pt的字体大小,还设置了overflow-y: scroll,可生成的PDF里,textarea的字体自动缩小到特别小,用的还是默认的Helvetica字体,而且也没有滚动功能。虽然手动用Adobe Acrobat Pro把字体从“自动”改成固定大小能解决,但这显然不是个可行的批量方案。
我已经试过好几种方式来应用样式了:先是在HTML里用内部<style>标签,然后试过给textarea加内联样式,最后还试了链接外部CSS文件(甚至同时设置了RenderingOptions.CustomCssUrl指向这个CSS文件),这些方法在Chrome里都能正常工作,但到了PDF渲染就失效了。目前我已经向Iron Software提交了工单。
效果对比
- HTML在Chrome中的显示:textarea样式正常,字体大小合适,符合CSS设置
- 渲染后的PDF在Chrome中的显示:textarea字体极小,样式完全不符合预期
我的疑问:
- 有没有什么我遗漏的设置,能让CSS样式成功覆盖PDF里textarea的默认渲染规则?
- 有没有后处理的方法,可以遍历生成的PDF里所有的textarea(也就是Tx字段),手动把字体大小从自动改成固定值?
我的代码
Python 代码
# render HTML as PDF with IronPDF import ironpdf ironpdf.License.LicenseKey = "license key goes here" html = """<!DOCTYPE html> <html lang="en"> <head> <title>Large Text Area Test</title> <link rel="stylesheet" type="text/css" href="/Users/username/styles/test2.css"> </head> <body> <h1>This is a heading</h1> <p>This is a paragraph.</p> <form name="testform"> <br><input type="checkbox" name="mycheckbox"> <label for="mycheckbox">Check the box.</label><br> <br>Enter some text: <br><textarea id="largetextarea" name="largetextarea" rows="20" cols="80"> Here is some text between textarea tags. </textarea> <br> </form> </body> </hmtl> """ renderer = ironpdf.ChromePdfRenderer() renderer.RenderingOptions.CreatePdfFormsFromHtml = True renderer.RenderingOptions.CustomCssUrl = '/Users/username/styles/test2.css' pdf = renderer.RenderHtmlAsPdf(html) pdf.SaveAs('largetextareacss.pdf')
CSS 文件(test2.css)
/* test2.css - Style sheet settings for PDF creation from HTML */ h1 { color: blue; font-family: cambria; font-size: 16pt; margin-top: 2em; } p { color: red; font-family: cambria; font-size: 12pt; margin-left: 1em; } body { color: black; font-family: cambria; font-size: 12pt; } textarea { color: blue; font-family: cambria; font-size: 12pt; overflow-y: scroll; }
备注:内容来源于stack exchange,提问作者BalooRM
相关产品推荐
相关产品推荐

