Ruby Prawn PDF 页眉页脚 checkbox 以及 style

liker · 2013年11月26日 · 最后由 hexawing 回复于 2013年12月22日 · 3719 次阅读
Prawn::Document.generate("#{Rails.root}/../dfs/metadata.pdf",
:info => {
:Title => "My title",
:Author => "John Doe",
:Subject => "My Subject",
:Keywords => "test metadata ruby pdf dry",
:Creator => "ACME Soft App",
:Producer => "Prawn",
:CreationDate => Time.now,
:Grok => "Test Property"
}) do
text "This is a test of setting metadata properties via the info option."
text "It allows one to specify non standard properties like 'Grok'."
end

页脚页眉怎么生成?

prawn 是纯 ruby 代码,可以用 ruby 的各种模块化方式组织,一个简单解决方法是用 load 执行:

load "./pdf_t.pdf.prawn"
Prawn::Document.generate("#{Rails.root}/../dfs/hello.pdf") do
   header margin_box.top_left do
       text "Here's My Fancy Header", :size => 25, :align => :center
   end

   text "hello world!"
end

ActionView::Template::Error (undefined method `header' for #Prawn::Document:0x007fb8e000a820):

Prawn::Document.generate('hello.pdf') do
  text("Hello Prawn!")
  start_new_page
  text("Hello Prawn!")
  start_new_page
  text("Hello Prawn!")
  number_pages "<page> in a total of <total>", :start_count_at => 0, :page_filter => :all, :at => [bounds.right - 100, 0], :align => :right, :size => 25
end
Prawn::Document.generate("test.pdf")  do
   repeat :all do

    # header
    bounding_box [bounds.left, bounds.top], :width  => bounds.width do
        #font "Helvetica"
        #text "Here's My Fancy Header", :align => :center, :size => 25
        data = [
            ["1","2","3"],
            ["2","3","4"]
            ]
        table(data)

        stroke_horizontal_rule
    end

    # footer
    bounding_box [bounds.left, bounds.bottom + 25], :width  => bounds.width do
        font "Helvetica"
        stroke_horizontal_rule
        move_down(5)
        text "And here's a sexy footer", :size => 16
    end
  end

  bounding_box([bounds.left, bounds.top - 50], :width  => bounds.width, :height => bounds.height - 100) do                 
   text "this is some flowing text " * 400
  end
end

已解决

新建一个 table 怎么判断这个 table 是否在一页 不在一页的话 新建一页建立 table?

多选框 https://gist.github.com/bradediger/58890

# "☐"    "\xE2\x98\x90"  "\u2610"
# "☑"    "\xE2\x98\x91"  "\u2611"
# "☒"    "\xE2\x98\x92"  "\u2612"
# "font" "#{Prawn::BASEDIR}/data/fonts/DejaVuSans.ttf"
def checkbox(label, checked)
    box = checked ? "\u2611" : "\u2610"
    checkbox_font = "#{Prawn::BASEDIR}/data/fonts/DejaVuSans.ttf"
    "<font name='app/assets/fonts/STSONG.TTF'>#{label}</font><font name='"+checkbox_font+"'>"+box+"</font>"
end
font "app/assets/fonts/STSONG.TTF"
# 如有多选框 并且默认字体是仿宋 会报错   bad font 我只能切换到以前的字体,谁有没有更好的方法
font "Helvetica"
table([
    [checkbox("读书",true),checkbox("书法",true),checkbox("绘画",false)]
], :cell_style => {:inline_format => true})

#5 楼 @liker 你是说 table 换页吗?prawn 的 table 默认就是换页的啊,还可以带上表头……

#7 楼 @hexawing 我的意思是 比如有一列占了四行,当第二行就分页时,显示的效果特别丑。

呃,Prawn 似乎只能做到这样了……

需要 登录 后方可回复, 如果你还没有账号请 注册新账号