HTML 到 PDF 的轨道
我尝试过 PDFKit 和 wicked_pdf。
PDFKit 几乎让我到达了我想要的地方,但它不支持 css 规则,因此我可以将页脚固定在底部:
position: fixed;
bottom: 0;
有解决此问题的解决方法吗?
谷歌搜索这个主题并没有给出太多信息,除了像虾之类的不支持 html 的东西。
对于那些想知道的人。 wkhtmltopdf 已安装并工作,因此 PDFKit 可以工作。
respond_to do |format|
format.html {render :layout => nil}
format.pdf do
render :pdf => "file_name.pdf",
:save_to_file => Rails.root.join('public/uploadedfiles', "filename")
end
end
I've tried PDFKit and wicked_pdf.
PDFKit almost got me where I wanted but it didn't have support for a css rule so that I could get the footer fixed in the bottom:
position: fixed;
bottom: 0;
Any workarounds to fix this?
Googling the subject doesn't give much except others like prawn and the likes with no html support.
For those who wonder. wkhtmltopdf is installed and works thus PDFKit works.
respond_to do |format|
format.html {render :layout => nil}
format.pdf do
render :pdf => "file_name.pdf",
:save_to_file => Rails.root.join('public/uploadedfiles', "filename")
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 CSS3 中,您可以使用
@page
规则以这种方式包含页脚:许多付费 PDF 解决方案都支持此功能,例如 PDFReactor 和我认为的 Prince。
但是,Webkit(
wkhtmltopdf
所基于的)不支持CSS3 分页媒体规则 因此您需要后备。Wicked PDF 文档列出了一个
:footer
选项,您可以使用它来传入在特定页面中呈现的 HTML 块。这看起来是您的最佳选择。In CSS3 you could use an
@page
rule to include footers in this way:This is supported in a lot of paid PDF solutions, e.g. PDFReactor and I think Prince.
However Webkit (which
wkhtmltopdf
is based on) doesn't support the CSS3 paged media rules so you need a fallback.The Wicked PDF documentation lists a
:footer
option which you can use to pass in chunks of HTML to render in particular pages. This looks like the best option for you.我没有尝试过 PDFKit 或 wicked_pdf 所以我只是想知道。但也许这可以解决问题。您是否尝试过使用粘性页脚 hack 来代替?
http://ryanfait.com/sticky-footer/
I haven't tried PDFKit nor wicked_pdf so I'm just wondering. But maybe this could do the trick. Have you tried using Sticky Footer hack instead?
http://ryanfait.com/sticky-footer/
我真的不确定为什么你想要在 pdf 文档中修复一个元素,这真的是你想要的吗?此外,这两个 gem 都使用了 wkhtmltopdf,这是一个利用 webkit 渲染 html 并最终将其转换为 pdf 的实用程序。一旦它在 webkit 上看起来很棒,那么它作为 pdf 看起来也应该很棒。但我不认为 pdf 支持像 CSS 中那样“固定”元素。
I am really not sure why you would want an element to be fixed in a pdf document, is that really what you want? Besides, both gems make use of wkhtmltopdf, which is an utility that makes use of webkit to render its html and ultimately convert it to pdf. Once it looks great on webkit, it should look great as pdf. But I dont think pdf supports elements to "fixed" as in CSS.
我建议查看 DocRaptor。根据我的经验,它比同类程序有更好的 CSS 支持。
DocRaptor
以下是将 DocRaptor 与 Rails 结合使用的代码链接:
DocRaptor Rails 示例
I'd recommend looking into DocRaptor. It's got better CSS support than comparable programs, from my experience.
DocRaptor
Here's a link to the code for using DocRaptor with Rails:
DocRaptor Rails example