PDFkit doesn't display pictures in PDF

别来无恙 提交于 2019-12-19 17:37:55

问题


Rails 2, PDFkit 0.5.0

Im generating a PDF from a View in Rails 2 with PDFkit and everything works fine. The only thing which doesn't work is displaying pictures in the pdf.

When I look at the View in the Browser, the picture is there but its missing in the PDF. There is only a placeholder existing in the PDF.

The image_tag is looking like this:

<%= image_tag('plus.gif') %>

I also tried to realize it with a css-file but it doesn't work either.

Any ideas?


回答1:


Because of the way that wkhtmltopdf works you need to specify the full path to any assets (JS, CSS, images etc), including the domain name.

This won't work:

<img src="/images/foo.png" />

This will:

<img src="http://example.com/images/foo.png" />

One workaround is to set an explicit asset host, even if it's the same server as your app is running on (see the AssetTagHelper documentation for details). Another would be to specify the hostname in the image_tag.




回答2:


Instead of putting the full path each time, you can add a base tag to the head section.

<base href="http://mydomain.com" target="_blank" />


来源:https://stackoverflow.com/questions/6108943/pdfkit-doesnt-display-pictures-in-pdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!