如何根据标题高度移动pdf报告的正文内容

时间:2015-03-09 17:59:35

标签: css ruby-on-rails pdf wicked-pdf

我有两个插件,其中一个插件用于设计报表的页眉和页脚,另一个插件用于编写pdf报表的正文内容。 当标题的高度增加或减少时,pdf报告的主体内容应根据标题的高度动态地上下移动。对此的任何解决方案都非常有用。

def custom_report_pdf
  begin
report = CustomReport.find(params[:id].to_i)
paper_margin = report.paper_margin
paper_orientation = report.paper_orientation 
paper_size = report.paper_size
unless paper_size.nil?
  @page_height = paper_size[:page_height]
  @page_width = paper_size[:page_width]
end
@student_ids = []
query = []
@sorted_description =[]
params[:custom_reports].each do |key,value|
  @student_ids << key if value.to_i == 1
end
@student_ids = CustomReport.sort_by_first_name(@student_ids)
@student_ids.each do |student_id|
    # the content of tinymce description part whole thing is splitted then it is made loop one by one as word
    description = report.description.split
    description.each_with_index do |word, i|
      @value = CustomReport.match_string(query, word, student_id, i, params[:id]).to_s
    end
    @sorted_description << @value
    query = []
  end
  #      if defined? HeaderFooterDesign == 'constant' and HeaderFooterDesign.first.config_key == true
  #        render :pdf => "#{report.name}"
  #      else
  render :pdf => "#{report.name}",
    :orientation => paper_orientation.nil? ? 'Portrait' : paper_orientation,
    :page_height => @page_height.nil? ? '27.94cm' : @page_height+"cm",
    :page_width => @page_width.nil? ? '21.59cm' : @page_width+"cm",
    :margin => paper_margin.nil? ? {:top => '3.2cm',:bottom => '2cm', :left => '2cm',:right => '2.1cm'} : {:top => (paper_margin[:top].to_f+ 3.5).to_s + "cm",:bottom => (paper_margin[:bottom].to_f + 0.2).to_s + "cm", :left => paper_margin[:left]+"cm",:right => (paper_margin[:right].to_f).to_s + "cm" }
  #      end
rescue Exception => e
  Rails.logger.info "Exception in design_custom_reports controller, custom_report_pdf action"
  Rails.logger.info e
  flash[:notice] = "Sorry, something went wrong. Please inform administration"
  redirect_to :action => :index
end

1 个答案:

答案 0 :(得分:0)

请参阅以下示例,了解如何使用bounding_box

pdf_file_path = "#{Rails.root}/tmp/#{pdf_file_name}.pdf"
Prawn::Document.generate(pdf_file_path, :top_margin => 0, :bottom_margin => 0) do
  font "Helvetica"
  repeat :all do
    # header
    bounding_box [bounds.left, bounds.top], :width => bounds.width, :height => 100 do
      move_down(5)
      text_box "103, 1st Floor, Bldg #42, Jasminium,", :align => :right, :size => 8, :at => [bounds.left, bounds.top - 10]
      text_box "link road extension,", :align => :right, :size => 8, :at => [bounds.left, bounds.top - 20]
      text_box "Bhujbal complex,", :align => :right, :size => 8, :at => [bounds.left, bounds.top - 30]
      text_box "pune", :align => :right, :size => 8, :at => [bounds.left, bounds.top - 40]
      image "#{Rails.root}/public/images/logo.png", :position => :left, :scale => 0.2
      stroke_horizontal_rule
    end

    # footer
    bounding_box [bounds.left, bounds.bottom + 65], :width  => bounds.width, :height => 80 do
      stroke_horizontal_rule
      move_down(5)
      image "#{Rails.root}/public/images/stamp.jpg", :at => [bounds.left, bounds.top - 2], :scale => 0.7
      text "www.google.com", :align => :center
    end
  end
    summary_cells = [[make_cell(:content => "", :background_color => "6E6E6E", :text_color => "FFFFFF"),
      make_cell(:content => "Overview", :background_color => "6E6E6E", :text_color => "FFFFFF"),
      make_cell(:content => "Status", :background_color => "6E6E6E", :text_color => "FFFFFF"),
      make_cell(:content => "Flag", :background_color => "6E6E6E", :text_color => "FFFFFF"),
      make_cell(:content => "Approval Type", :background_color => "6E6E6E", :text_color => "FFFFFF")]]
    @students.each do |b|
      color = case b.status
      when "Active" then "3BA936"
      when "InActive" then "C80000"
      when "InProgress" then "FA9632"
      else "6E6E6E"
      end
      students << b.documents.not_auto.visible
      summary_cells << [b.class.to_s, b.overview, b.completion_status, make_cell(b.status.titleize, :text_color => color), b.linked_task.try(:means_of_approval)]
    end
end