WickedPDF 第一页的不同边距设置? [英] WickedPDF different margin settings for first page?
问题描述
使用:
wkhtmltopdf-binary 0.9.9.1wicked_pdf 0.10.2导轨 4.1.7红宝石 2.1.3OS X 10.10.1(64 位)
我有一个需要一点空间的标题,所以我不得不使用 :margin =>{ :top =>40 }
(连同标题间距)在我的 WickedPDF 选项中.但是,我不想在我的第一页使用这个,因为页眉仅用于第 2 页及以上.我如何实现这一目标?
尝试
对于第一页的内容,我尝试通过 CSS:
位置:绝对;顶部:-20;
这会导致内容被边距区域剪裁".似乎应用
:margin =>{:top=>40}
在标题片段和 PDF 内容之间创建一个白色块状矩形.我尝试对第一页使用
:cover
选项.不幸的是,边距设置也适用于封面模板.此外,wkhtmltopdf 0.12 似乎存在语法差异,因此它会中断
我能想到的一些解决方案:
- 创建 2 个单独的 PDF 并将它们连接在一起.需要找到PDF合并gem等
- 通过 Javascript 生成持久标头.老实说,这很棘手.特别是当有表格中断时(表格高度超过一页).此外,每个内容部分的标题都不同.我目前通过 wkhtmltopdf javascript 助手(
frompage
、topage
、page
、section
等)进行此工作
任何帮助将不胜感激.
添加一个将 --header-html
设置为以下内容的脚本:
<头><脚本>函数 subst() {var vars = {};var x = document.location.search.substring(1).split('&');for (var i in x) {var z=x[i].split('=',2);vars[z[0]] = unescape(z[1]);}if(vars['page'] == 1) {//如果页面为 1,则不显示标题document.getElementById("doc-header").style.display = 'none';}}头部><body onload="subst()"><div id="doc-header">...您的内容在这里...
</html>
这应该适用于最新的 wkhtmltopdf 稳定版本 (0.12.1) -- 此示例取自 此版本中已修复的崩溃.
Using:
wkhtmltopdf-binary 0.9.9.1
wicked_pdf 0.10.2
rails 4.1.7
ruby 2.1.3
OS X 10.10.1 (64-bit)
I have a header that takes a bit of space, so I had to use :margin => { :top => 40 }
(along with header spacing) in my WickedPDF options. However, I don't want this for my first page, because the header is only used on page 2 and above. How do I achieve this?
Attempts
For contents in the first page, via CSS I tried:
position: absolute; top: -20;
Which results in the content being "clipped" by the margin region. It seems that applying
:margin => {:top=>40}
creates a blocking white rectangle between the header fragment and the PDF contents.I tried using the
:cover
option for the first page. Unfortunately, the margin setting is also applied to the cover template. Also, there seems to be a syntax difference in wkhtmltopdf 0.12 so it breaks
Some solutions I can think of:
- Create 2 separate PDF and join them together. Need to find PDF merging gems, etc.
- Generate persistent header via Javascript. This is quite tricky to be honest; especially when there are table breaks (table height is more than one page). Also, the header is different for every content sections. I currently have this working via wkhtmltopdf javascript helpers (
frompage
,topage
,page
,section
, etc.)
Any help would be gladly appreciated.
Add a script with the --header-html
set to the following:
<!DOCTYPE html>
<html>
<head>
<script>
function subst() {
var vars = {};
var x = document.location.search.substring(1).split('&');
for (var i in x) {var z=x[i].split('=',2);vars[z[0]] = unescape(z[1]);}
if(vars['page'] == 1) { // If page is 1, do not show the header
document.getElementById("doc-header").style.display = 'none';
}
}
</script>
</head>
<body onload="subst()">
<div id="doc-header">
... your content here ...
</div>
</body>
</html>
This should work with the latest wkhtmltopdf stable release (0.12.1) -- this sample was taken from a crash which was fixed in this release.
这篇关于WickedPDF 第一页的不同边距设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!