Prawn: テキストの配置を理解するためのより簡単な方法。

作成日 2016年04月16日  ·  4コメント  ·  ソース: prawnpdf/prawn

`generate_pdf 'というメソッドを持つクラスがあります。コードは次のとおりです。

def generate_pdf
    pdf = Prawn::Document.new(:page_size=> "A4",:top_margin => 80,:bottom_margin => 40,:template => 'public/medicaid.pdf')
    pdf.repeat([1]) do
    pdf.draw_text "#{@call.patient_name}", :at => [200,200], :size => 10
    end
    pdf.text "#{@call.patient_dob.strftime("%m/%d/%Y")}"
    pdf.repeat([2]) do
      pdf.draw_text "#{@call.transfer_date.strftime("%m/%d/%Y")}", :at => [200,200]
    end
    pdf.repeat([2]) do
      pdf.draw_text "#{@call.transferred_from.facility_name}", :at => [100, 100]
    end
    pdf.text "#{@call.transferred_from.npi}"
    pdf.text "#{@call.transferred_from.tpi}"
    pdf.text "#{@call.transferred_from.taxonomy}"
    pdf.text "#{@call.caller_name}"
    pdf.render_file("public/uploads/faxes/patient_#{@call.patient_name}_#{Time.zone.now}.pdf")
  end

PDFテンプレートに基づいた3ページのフォームがあり、問題なく生成されます。 しかし、私はdraw_textメソッドで、特に:at => [0,0]を使用してポジショニングを把握するのに非常に苦労しています。

各draw_text要素のX / Y軸を簡単に把握できるようなツールやオンラインリソースはありますか? すべてのテキストフィールドをPDFの特定の位置に適切に配置する必要がありますが、グリッドシステムの先頭または末尾を作成するのに苦労しています。 このためのUIがあればいいのにと思います。 :)テキストを変更するたびに、コンソールでクラスを再読み込みし、PDFを再生成して、近くにいるかどうかを確認する必要があります。 これには数え切れないほどの時間がかかります。 これを手動で行うよりも良い方法はありますか?

最も参考になるコメント

これは、要素の測定と配置に役立つグリッドコードです。

require "prawn"

module Prawn
  module Graphics
    def stroke_grid(options = {})
      options = {
        :at => [0, 0],
        :height => bounds.height.to_i - (options[:at] || [0, 0])[1],
        :width => bounds.width.to_i - (options[:at] || [0, 0])[0],
        :step_length => 50,
        :negative_axes_length => 0,
        :color => "000000"
      }.merge(options)

      Prawn.verify_options([:at, :width, :height, :step_length,
                            :negative_axes_length, :color], options)

      save_graphics_state do
        fill_color(options[:color])
        stroke_color(options[:color])

        stroke_bounds

        fill_circle(options[:at], 1)

        (options[:step_length]..options[:width]).step(options[:step_length]) do |point|
          fill_circle([options[:at][0] + point, options[:at][1]], 1)
          draw_text(point, :at => [options[:at][0] + point - 5, options[:at][1] + 10], :size => 7)
        end

        (options[:step_length]..options[:height]).step(options[:step_length]) do |point|
          fill_circle([options[:at][0], options[:at][1] + point], 1)
          draw_text(point, :at => [options[:at][0] + 10, options[:at][1] + point - 2], :size => 7)
        end

        self.line_width = 0.5
        (0..options[:height]).step(5) do |point|
          tp = point % 50 == 0 ? 0.5 : 0.1
          transparent(tp) do
            stroke_horizontal_line(options[:at][0],
                                   options[:at][0] + options[:width], :at => options[:at][1] + point)
          end
        end

        (0..options[:width]).step(5) do |point|
          tp = point % 50 == 0 ? 0.5 : 0.1
          transparent(tp) do
            stroke_vertical_line(options[:at][1],
                                 options[:at][1] + options[:height], :at => options[:at][0] + point)
          end
        end
      end
    end
  end
end

Prawn::Document.generate("grid.pdf") do
  # Remove canvas to draw grid within current bounds
  canvas do
    stroke_grid
  end
end

全てのコメント4件

ああ、これがスタックの質問である場合は事前に謝罪しますが、ここの人々はすぐに答えられるだろうと思いました。 乾杯! 👍

こんにちは、まず第一に、あなたはコアprawnpdfの一部ではないテンプレートを使用しています。 ポジショニングが簡単になるように、すべてのpdfをエビで生成することをお勧めします。
そうでなければ、あなたが使用している方法以外に方法はありません。 テンプレートの上に5x5のグリッドを印刷して、配置に役立てることができます...

ご回答ありがとうございます。 ガイドラインがあるようにグリッドを印刷する最良の方法は何ですか? ドキュメントを読みましたが、カバーすることがたくさんあります。 :)

これは、要素の測定と配置に役立つグリッドコードです。

require "prawn"

module Prawn
  module Graphics
    def stroke_grid(options = {})
      options = {
        :at => [0, 0],
        :height => bounds.height.to_i - (options[:at] || [0, 0])[1],
        :width => bounds.width.to_i - (options[:at] || [0, 0])[0],
        :step_length => 50,
        :negative_axes_length => 0,
        :color => "000000"
      }.merge(options)

      Prawn.verify_options([:at, :width, :height, :step_length,
                            :negative_axes_length, :color], options)

      save_graphics_state do
        fill_color(options[:color])
        stroke_color(options[:color])

        stroke_bounds

        fill_circle(options[:at], 1)

        (options[:step_length]..options[:width]).step(options[:step_length]) do |point|
          fill_circle([options[:at][0] + point, options[:at][1]], 1)
          draw_text(point, :at => [options[:at][0] + point - 5, options[:at][1] + 10], :size => 7)
        end

        (options[:step_length]..options[:height]).step(options[:step_length]) do |point|
          fill_circle([options[:at][0], options[:at][1] + point], 1)
          draw_text(point, :at => [options[:at][0] + 10, options[:at][1] + point - 2], :size => 7)
        end

        self.line_width = 0.5
        (0..options[:height]).step(5) do |point|
          tp = point % 50 == 0 ? 0.5 : 0.1
          transparent(tp) do
            stroke_horizontal_line(options[:at][0],
                                   options[:at][0] + options[:width], :at => options[:at][1] + point)
          end
        end

        (0..options[:width]).step(5) do |point|
          tp = point % 50 == 0 ? 0.5 : 0.1
          transparent(tp) do
            stroke_vertical_line(options[:at][1],
                                 options[:at][1] + options[:height], :at => options[:at][0] + point)
          end
        end
      end
    end
  end
end

Prawn::Document.generate("grid.pdf") do
  # Remove canvas to draw grid within current bounds
  canvas do
    stroke_grid
  end
end
このページは役に立ちましたか?
0 / 5 - 0 評価