Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Easier way to figure out text positioning. #957

Closed
nynhex opened this issue Apr 16, 2016 · 4 comments
Closed

Easier way to figure out text positioning. #957

nynhex opened this issue Apr 16, 2016 · 4 comments

Comments

@nynhex
Copy link

nynhex commented Apr 16, 2016

I have a class with a method called `generate_pdf', the code is below:

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

I've got a 3 page form based off a PDF template which generates just fine. But I'm having a really hard time with the draw_text method specifically figuring out the positioning using :at => [0,0]

Is there some sort of tool or online resource that will make figuring out the X/Y axis for each draw_text element easier? I need all of my text fields aligned properly on the pdf in certain positions but am having a hard time making heads or tails of the grid system. I wish there was a UI for this. :) Each time I make a text change I have to reload the class in the console and regenerate the PDF to see if I'm close. This is taking countless hours. Is there a better way than doing this manually?

@nynhex
Copy link
Author

nynhex commented Apr 16, 2016

Oh and I apologize in advance if this should be a stack question, but I figured the folks here would have a quick answer. Cheers! 👍

@bvogel
Copy link

bvogel commented Apr 16, 2016

Hi, first of all, you are using templates which isn't part of core prawnpdf. My recommendation would be generating all of the pdf in prawn so positioning will be easy.
Otherwise there is no way apart from the way you are using. You may print a grid at 5x5 on top of the template to help positioning...

@nynhex
Copy link
Author

nynhex commented Apr 16, 2016

Thanks so much for your answer. What's the best way to print a grid so I have a guideline? I've read the docs but there's a lot to cover. :)

@nynhex nynhex closed this as completed Apr 16, 2016
@iamjohnford
Copy link
Contributor

Here's the grid code I use to help measure and position elements.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants