代码之家  ›  专栏  ›  技术社区  ›  rip747

Rails:将HTML转换为PDF?

  •  56
  • rip747  · 技术社区  · 16 年前

    与在ColdFusion上使用CFDOCUMENT类似,将HTML转换为PDF的最佳和最简单的方法是什么?

    更新: 我真的很感激人们到目前为止给出的所有评论和建议,但是我觉得留下答案的人没有抓住要点。

    2) 他们必须能够接收html(来自文件、url或字符串变量),然后生成pdf。像prawn、rprf、rtex这样的库是使用它们自己的方法生成的,而不是采用html。

    15 回复  |  直到 16 年前
        1
  •  67
  •   Joshua Pinter    10 年前

    宝石, WickedPDF 确切地 那个

        2
  •  15
  •   whirlwin    12 年前
        3
  •  12
  •   Sajjad Murtaza    8 年前

    Try WickedPDF ,PDF生成器(来自HTML)插件。

    下面是一个例子

    gem 'wicked_pdf'  
    gem 'wkhtmltopdf-binary' 
    #wicked_pdf is a wrapper for wkhtmltopdf, you'll need to install that, too
    

    在您的控制器中(我想您是在show method中):

        respond_to  do |format|
          format.html
          format.pdf do
            pdf = render_to_string :pdf => 'test',
                             layout: 'pdf.html.erb',
                             template: 'show.pdf.slim',
                             header: { :right => '[page] of [topage]'},
                             margin: {top: 0,
                                      bottom: 0,
                                      left: 0,
                                      right: 0},
                             outline: {outline: true,
                                       outline_depth: 2}
            end
         end
    

    = link_to 'Download Pdf', you_show_path(@uour_object, format: :pdf)
    

    如果您想在电子邮件附件中发送pdf,请先将pdf文件保存在公用文件夹或临时文件夹中,然后保存在mailer类中(例如,在mailers/e_mailer.rb中)

    attachments["test.pdf"] = File.read(Rails.root.join('public',"test.pdf"))
    mail(:to => to, :cc => cc , :subject => "subject")
    

    File.delete(Rails.root.join("public", "test.pdf"))
    
        4
  •  3
  •   Michael    11 年前
        5
  •  2
  •   tsdbrown    15 年前

    在经历了大量的血汗和眼泪之后,我成功地编写了一个非常简单的rails应用程序,允许用户通过 TinyMce ,这些文件将被保存,并可作为pdf文档查看。

    有一个包装HTMLDOC的ruby gem,您需要: PDF::HTMLDoc

    一旦您获得了,请注册mime类型,然后您可以执行以下操作:

    @letter_template = LetterTemplate.find(params[:id])
    
    respond_to do |format|
          format.html
          format.pdf { send_data render_to_pdf({:action => 'show.rpdf',  :layout => 'pdf_report'}), :filename => @letter_template.name + ".pdf",  :disposition => 'inline' }
        end
    

    def render_to_pdf(options =nil)
        data = render_to_string(options)
        pdf = PDF::HTMLDoc.new
        pdf.set_option :bodycolor, :white
        pdf.set_option :toc, false
        pdf.set_option :portrait, true
        pdf.set_option :links, false
        pdf.set_option :webpage, true
        pdf.set_option :left, '2cm'
        pdf.set_option :right, '2cm'
        pdf.set_option :footer, "../"
        pdf.set_option :header, "..."
        pdf.set_option :bottom, '2cm'
        pdf.set_option :top, '2cm'
        pdf << data
        pdf.generate
      end
    

        6
  •  1
  •   mat    16 年前
        7
  •  1
  •   rip747    16 年前

    我找到的最接近这个“解决方案”是使用JRuby,然后使用 The Flying Saucer Project . 我只是希望它能移植到Ruby,这样它就可以成为一个本地解决方案。上帝啊,我希望我在爪哇能学得更好。

        8
  •  0
  •   Thomas    14 年前

    项目可能停滞不前,但Pisa(又名 XHTML2PDF )是一个Python库,它将CSS+HTML转换为PDF。不久前,将其作为命令行工具在Rails应用程序(低流量)中使用,效果非常好。

        9
  •  0
  •   Johnny    7 年前

    我想向您推荐HTML到PDF的解决方案 https://grabz.it .

    他们有一个非常灵活且易于使用的屏幕截图API,可以被任何类型的Ruby应用程序使用。

    如果你想试试,首先你应该得到授权 app key + secret development free SDK

    然后,在您的应用程序中,实施步骤将是:

    //initialization    
    require 'grabzit'
    grabzItClient = GrabzIt::Client.new("APPLICATION KEY", "APPLICATION SECRET")
    
    //capturing the url to PDF
    grabzItClient.url_to_pdf("http://www.google.com")   
    

    接下来是储蓄。您可以使用两种保存方法之一, save

    grabzItClient.save("http://www.example.com/handler/index")  
    

    save_to 如果需要同步保存(这将迫使应用程序在创建屏幕截图时等待):

    filepath = "images/result.jpg"
    grabzItClient.save_to(filepath) 
    

    检查文档中的 details

    image screenshots

        10
  •  -1
  •   Johan Dahlin Idelic    16 年前

        11
  •  -1
  •   Milan Novota    16 年前

    在Ruby中肯定没有直接的方法可以做到这一点(至少在几个月前我尝试这样做时没有)。如果您确实无法避免从html生成PDF,那么您可能应该四处寻找一些用于此目的的外部工具或服务。

    祝你好运

        12
  •  -1
  •   Adam Yocum Adam Yocum    16 年前

    我正在www.pd4ml.com上尝试图书馆

    这是一个代码剪贴。。。。

    我将html拉入字符串内容。然后我做了一些事情,比如用复选框替换单选按钮,然后在pd4ml库中运行它来呈现pdf。
    结果看起来与原始页面非常相似。。。。

        String content = nextPage.generateResponse().contentString();
    
        content = content.replace("Print", "");
        content = content.replace("Back", "");
    
        content = content.replace("border=\"1\"", "border=\"0\"");
        content = content.replace("radio", "checkbox");
    
        java.net.InetAddress i = java.net.InetAddress.getLocalHost();
        String address =  i.getHostAddress()+":53000";
    
        content = content.replace("img src=\"/cgi-bin", "img src=\"http://"+address+"/cgi-bin");
    
        System.out.println(content);
    
        PD4ML html = new PD4ML();
        html.setPageSize( new java.awt.Dimension(650, 700) );
        html.setPageInsets( new java.awt.Insets(30, 30, 30, 30) );
        html.setHtmlWidth( 750 );
        html.enableImgSplit( false );
        html.enableTableBreaks(true);
    
        StringReader isr = new StringReader(content);
        baos = new ByteArrayOutputStream();
        html.render( isr, baos);
        PDFRegForm pdfForm = (PDFRegForm)pageWithName("PDFRegForm");
        pdfForm.baos = baos;
        pdfForm.generateResponse();
    
        13
  •  -1
  •   apolinsky    16 年前

    http://html2pdf.seven49.net/Web/

    还有一个sourceforge项目。

    艾伦

        14
  •  -1
  •   Aleksandr Kovalev Marty    9 年前

    HTMLDOC ?

    • 免费和开源(提供商业支持);
      • …独立应用程序,
      • …或作为基于web的服务运行;
        15
  •  -1
  •   Aleksandr Kovalev Marty    9 年前

    不是特定于Ruby的,但我发现最好的解决方案是开源 HTMLDOC .