2

脚注付きの PDF ドキュメントをレンダリングする方法を探しています。脚注のテキストは、脚注の参照と同じページのフッターに表示されます (ドキュメントの最後ではありません)。この種の脚注は、翻訳者のコメントなど、書籍に表示されます。脚注参照を脚注テキストにリンクできるかどうかはオプションです。

これまでのところ、Prawn と PDFKit を見てきましたが、簡単な解決策が見つからないようです。

これは私がやろうとしていることのサンプルです: footnote sample

これが私が Prawn を使って思いついたものです。明らかに、コーナーケースをカバーするには、これにはさらに作業が必要です。そのルートをさらに下る価値があるのか​​ 、それともラテックスのようなまったく異なるものを試す価値があるのか​​ 疑問に思っています(ルビーでは使用していません)。

Prawn::Document.generate("foo.pdf",:page_size => 'A4') do
  #read 150 lorem ipsum records of various length 
  records = File.read(
    Rails.root.join("lib/assets/lorem_ipsum_paragraphs.txt")
  ).split("\n").reject(&:blank?)
  #assign some random footnotes to the paragraphs
  footnotes = {
    1 => '* ' + records[120],
    2 => '* ' + records[54],
    11 => '* ' + records[2]
  }
  #this array will hold the current footnotes to draw
  #the assumption being we'll draw them on the current page as soon as we
  #reach the part of the page where we need to fit those
  footnotes_to_draw = []
  #this will hold the amount of space required to draw the footnotes
  space_needed = 0
  for i in 0..records.length
    str = records[i]
    if footnotes.keys.include? i
      str += '*'#this one has a footnote attached
      footnotes_to_draw << footnotes[i]
      space_needed += (height_of(footnotes[i]) + 15)
    end
    text "#{str}"
    if space_needed > 0
      #that means we will need to draw a footer on this page
      space_available = cursor
      puts "space needed: #{space_needed}, space available: #{space_available}"
      #check if we can still draw the next record, or now's the time'
      unless space_available - space_needed > height_of(records[i+1])
        puts "draw footer now"
        bounding_box [0,space_needed], :width => bounds.width, :height => space_needed do
          stroke_horizontal_rule
          footnotes_to_draw.each do |footnote|
            pad(10){text footnote}
          end
        end
        #reset current footnotes
        footnotes_to_draw = []
        space_needed = 0
        move_down space_needed
      end
    end
  end
end
4

0 に答える 0