Ruby on rails Rails对虾图表未完全呈现在页面底部

Ruby on rails Rails对虾图表未完全呈现在页面底部,ruby-on-rails,ruby,prawn,Ruby On Rails,Ruby,Prawn,我正在创建一个pdf报告,以便使用squid gem显示一些数据。这将允许我在pdf中显示图表。 我发现的唯一问题是,当图表不适合页面底部时,它看起来部分呈现,这一点都不好。你知道我该怎么解决这个问题吗 下面是我用来渲染图表的代码 require 'squid' class SurveyPdf < Prawn::Document def initialize(survey, view) super() font "#{Rails.root}/app/assets/fo

我正在创建一个pdf报告,以便使用squid gem显示一些数据。这将允许我在pdf中显示图表。 我发现的唯一问题是,当图表不适合页面底部时,它看起来部分呈现,这一点都不好。你知道我该怎么解决这个问题吗

下面是我用来渲染图表的代码

require 'squid'

class SurveyPdf < Prawn::Document
  def initialize(survey, view)
    super()
    font "#{Rails.root}/app/assets/fonts/roboto-condensed.ttf"

    @survey = survey
    @view = view
    questions
  end

  def questions
    @survey.questions.each do |question|
      text "#{question.title}", size: 20
      text "Answers #{question.answers.size}", size: 15
      if ["single", "select"].include? question.question_type.prefix
        if question.answers.choice_counter.any?
          chart choices: question.answers.choice_counter
        end
      end
      if question.question_type.prefix == "image"
        if question.answers.image_counter.any?
          chart images: question.answers.image_counter
        end
      end
      if question.question_type.prefix == "multiple"
        if question.answers.multiple_choice_counter.any?
          chart choices: question.answers.multiple_choice_counter
        end
      end
      if question.question_type.prefix == "raiting"
        move_down 5
        if question.answers.any?
          text_box "Average rating", size: 12, width: 120, :at => [0,  cursor - 2]
          text_box "#{average_rating(question.answers.rating_average)}", size: 12, width: 120, :at => [4 * 30,  cursor - 2]
        else
          text_box "Average rating", size: 12, width: 120, :at => [0,  cursor - 2]
          text_box "0", size: 12, width: 120, :at => [4 * 30,  cursor - 2]
        end
      end
    end
  end
end
对于类似的问题,我使用了

它预先呈现您在组块中放置的任何内容,以测试它是否适合当前页面。如果没有,它将跳到下一页并呈现

在您的情况下,您可以执行以下操作:

def questions
  @survey.questions.each do |question|
    group :too_tall => lambda { start_new_page } do |g|
      g.text "#{question.title}", size: 20
      g.text "Answers #{question.answers.size}", size: 15
      if ["single", "select"].include? question.question_type.prefix
        if question.answers.choice_counter.any?
          g.chart choices: question.answers.choice_counter
        end
      end
      if question.question_type.prefix == "image"
        if question.answers.image_counter.any?
          g.chart images: question.answers.image_counter
        end
      end
      if question.question_type.prefix == "multiple"
        if question.answers.multiple_choice_counter.any?
          g.chart choices: question.answers.multiple_choice_counter
        end
      end
      if question.question_type.prefix == "raiting"
        move_down 5
        if question.answers.any?
          g.text_box "Average rating", size: 12, width: 120, :at => [0,  cursor - 2]
          g.text_box "#{average_rating(question.answers.rating_average)}", size: 12, width: 120, :at => [4 * 30,  cursor - 2]
        else
          g.text_box "Average rating", size: 12, width: 120, :at => [0,  cursor - 2]
          g.text_box "0", size: 12, width: 120, :at => [4 * 30,  cursor - 2]
        end
      end
    end
  end
end
免责声明:我从未使用过squid,所以我唯一不确定的是g.chart。如果您有问题,请告诉我,我会尽力解决

鱿鱼更新

对虾分组宝石不知道像查特这样的乌贼方法。因此,我们可以从对虾分组宝石中提取逻辑,并将其直接添加到您的调查中。从应用程序中复制第7-63行,并从应用程序中删除对虾分组宝石

如果你想知道为什么会这样

Squid使用Prawn::Document.extensions方法强制Prawn::Document继承Squid方法。你可以在squid gem代码中看到这一点


为了使对虾分组有效,它将创建一个新的对虾::文档作为group方法的一部分。你可以看到。问题是通过Prawn分组gem实例化的Prawn::文档没有继承squid方法。。。但是您的SurveyPdf Prawn::Document实例确实继承了squid方法,因此,通过将分组逻辑添加到SurveyPdf类中,现在在您的group方法中实例化的Prawn::Document将起作用。

要回答您的注释中关于确定页面大小的问题,我将通过一些有用的方法来进行说明:

d = Prawn::Document.new
d.y #full page height
d.margin_box.bottom #actually top since prawn starts at the bottom
d.margin_box.absolute_bottom #actual top with margins included
d.margin_box.top #usable page height
d.margin_box.absolute_top #same as #y 
d.cursor #where you are vertically on the page
因此,您可以使用一些基本数学来确定适合度:

#this is all chart does excepting chart calls #draw 
#which we don't want to do until we determine if it fits 
c = Squid::Graph.new(d, choices: question.answers.choice_counter) 

#determine if the chart will fit vertically 
#if not start a new page and move to the top
unless d.cursor + c.height < d.margin_box.top
   d.start_new_page
   d.move_cursor_to(0)
end

#draw the chart onto the appropriate page
c.draw

希望这能在某种程度上有所帮助

解决方案我将使用它来确定图表是否能容纳剩余空间,如果不能,则添加一个页面并在那里渲染。e、 当前光标位置+图表高度>页面底部页面底部是什么?如果可能的话,你能给我看一些代码吗?我添加了一个答案来回答你的评论,而不是你的问题,但这应该对我在使用g.chartOK时得到的未定义方法“chart”有所帮助。我想我已经找到了答案。您需要从第7行复制到第63行,从复制到您的调查中。然后从应用程序中删除分组gem并重新运行。如果这样做很有效,肯定会成功;我会解释并更新我的答案,所以我尝试了你所说的,但我看不出这段代码有什么不同。图表应该跳转到下一页,而不是组do | g |这样做:组:太高=>lambda{start_new_page}do | g |。这将迫使它针对相同的内容开始一个新页面。。也许将图表移动到一个跨度块或类似的地方?如果842-cursor+250>842 start\u new\u页面,我会使用类似的方法修复它,否则保持不变_page@adavia很高兴你让它工作了。很抱歉,我花了一点时间才回复您,我以前从未使用过squid,在给出合理的答案之前,我必须做一点源代码审查来确定图表是如何实现的。