首页 文章

使用wicked_pdf生成pdf时进行分页

提问于
浏览
4

sample.erb.html

<p>Page 1</p1>

<p>Page 2</p2>

所以, "Page 1" 之后的所有内容我想在第二页上打印 .

我怎样才能做到这一点?

在_1357289中有一个解决方案,但它对我不起作用 .

例如,在Prawn的情况下,它有一个很好的功能叫做 start_new_page

3 回答

  • 1

    在你的CSS

    p{page-break-after: always;}
    

    更新

    在几个问题之后,我将扩展我的答案以及我如何在我的应用程序中使用它 .

    1有时候,wickedpdf帮助器不起作用,所以我添加了一个初始化器

    config /初始化/ wiked_pdf.rb

    module WickedPdfHelper
      def wicked_pdf_stylesheet_link_tag(*sources)
        sources.collect { |source|
          "<style type='text/css'>#{Rails.application.assets.find_asset("#{source}.css")}</style>"
        }.join("\n").gsub(/url\(['"](.+)['"]\)(.+)/,%[url("#{wicked_pdf_image_location("\\1")}")\\2]).html_safe
      end
    
      def wicked_pdf_image_tag(img, options={})
        image_tag wicked_pdf_image_location(img), options
      end
    
      def wicked_pdf_image_location(img)
        "file://#{Rails.root.join('app', 'assets', 'images', img)}"
      end
    
      def wicked_pdf_javascript_src_tag(source)
        "<script type='text/javascript'>#{Rails.application.assets.find_asset("#{source}.js").body}</script>"
      end
    
      def wicked_pdf_javascript_include_tag(*sources)
        sources.collect{ |source| wicked_pdf_javascript_src_tag(source) }.join("\n").html_safe
      end
    
      WickedPdf.config = {
    
      }
    end
    

    2在应用程序控制器中,使用常规配置参数创建配置方法

    app /控制器/ application_controller.rb

    class ApplicationController < ActionController::Base
      def pdf_config
        WickedPdf.config = {
            :wkhtmltopdf => "/usr/local/bin/wkhtmltopdf",
            :orientation  => 'Landscape',
            :layout => "pdf.html",
            :footer => {
                :left => "Rectores Lideres Transformadores",
                #:left => "#{Entidad.find(@current_user.entidad).nombre}",
                :right => "#{Time.now}",
                :font_size => 5,
                :center => '[page] de [topage]'
            },
            :disposition => 'attachment'
    
        }
      end
    
    end
    

    3为所有pdf文件创建公共布局 . 在这里,我使用我的应用程序css,以便在pdf报告中保持相同的网页外观和感觉,只有我必须使用相同的类和id的

    应用程序/布局/ pdf.html.erb

    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
      <%= wicked_pdf_stylesheet_link_tag "application" %> ----- HERE YOUR APPLICATION CSS -----
    </head>
    <div id="content">
      <%= yield %>
    </div>
    </body>
    </html>
    

    4在控制器中添加pdf重定向

    app /控制器/ users_controller.rb

    def index
        @users = User.all
    
        respond_to do |format|
          format.pdf do
            pdf_config
            render  :pdf => "filename"
          end
        end
      end
    

    5现在,在你的css中,选择哪个html id是页面制动

    #brake{page-break-after: always;}
    
  • 3

    我有同样的问题,我发现了一些可能有帮助的东西 . 这是我的分页CSS代码:

    .page-break {
      display: block;
      clear: both;
      page-break-after: always;
    }
    

    由于 TWO 原因,这不起作用:

    I. In one of the SASS imported file I had this line of code:

    html,身体
    overflow-x:hidden!important

    II. The other problem was bootstrap

    @import "bootstrap"
    

    它看起来像是因为 float: left

    .col-xs-1, .col-xs-2, .col-xs-3, .col-xs-4, .col-xs-5, .col-xs-6, .col-xs-7, .col-xs-8, .col-xs-9, .col-xs-10, .col-xs-11, .col-xs-12 {
      float: left;
    }
    

    分页符不再有效 . 所以,只需添加此 after 即可导入引导程序 .

    .col-xs-1, .col-xs-2, .col-xs-3, .col-xs-4, .col-xs-5, .col-xs-6, .col-xs-7, .col-xs-8, .col-xs-9, .col-xs-10, .col-xs-11, .col-xs-12 {
      float: initial !important;
    }
    
  • 0

    对于仍有这个问题但没有这些答案的人,就像我一样,只是不工作:我放弃使用CSS来修复分页符,而是生成2个pdf并将它们合并在一起并使用生成的文件代替,没有可能的方法让分页符不存在 .

    为了合并 files ,我使用了一个pdf文件名数组

    system("pdftk #{files.join(' ')} cat output merged_file_name.pdf")
    

相关问题