【问题标题】:Generating pdf from html using Prawn使用 Prawn 从 html 生成 pdf
【发布时间】:2013-09-23 04:21:26
【问题描述】:

我想从 html 生成 pdf。 gem prawn 似乎是最受欢迎的。我下载了它的手册,但是没有关于如何从 html 生成 pdf 的信息。

特别是,我还需要它在 Heroku 上工作,但这是第二个目标。

那么如何使用Prawnhtml生成pdf?

【问题讨论】:

    标签: ruby-on-rails ruby pdf-generation prawn


    【解决方案1】:

    寻找Pdfkit,它是第二受欢迎的宝石RubyToolbox。它使用 wkhtmltopdf 从 HTML 生成 PDF。在RailsCasts 上是一个较旧的教程。

    【讨论】:

    • 你知道我是否可以在 Heroku 上毫无问题地使用它吗?
    • 不幸的是我不知道,但它看起来应该可以正常工作。它们是由 Pdfkit 维护者在 github 上维护的 heroku 用户的 wkhtml 分支
    • 它是否支持Unicode?我需要使用西里尔字母。
    • 但是,我有错误 The switch --print-media-type, is not support using unpatched qt, and will be ignored (webrick) 或者它只是挂起并且连接在一段时间后重置(独角兽)。
    【解决方案2】:

    这是一个使用虾从 html 生成 pdf 的示例代码

    /// <summary>
    /// Convert the HTML code from the specified URL to a PDF document
        and send the document to the browser
    /// </summary>
    private void ConvertURLToPDF()
    {
        string urlToConvert = textBoxWebPageURL.Text.Trim();
    
        // Create the PDF converter. Optionally the HTML viewer width can
            be specified as parameter
        // The default HTML viewer width is 1024 pixels.
        PdfConverter pdfConverter = new PdfConverter();
    
        // set the license key - required
        pdfConverter.LicenseKey = "R8nYyNnI2MjRxtjI29nG2drG0dHR0Q==";
    
        // set the converter options - optional
        pdfConverter.PdfDocumentOptions.PdfPageSize = PdfPageSize.A4;
        pdfConverter.PdfDocumentOptions.PdfCompressionLevel = PdfCompressionLevel.Normal;
        pdfConverter.PdfDocumentOptions.PdfPageOrientation = PdfPageOrientation.Portrait;
    
    
        // set if header and footer are shown in the PDF - optional - default
            is false 
        pdfConverter.PdfDocumentOptions.ShowHeader = cbAddHeader.Checked;
        pdfConverter.PdfDocumentOptions.ShowFooter = cbAddFooter.Checked;
        // set if the HTML content is resized if necessary to fit the PDF
            page width - default is true
        pdfConverter.PdfDocumentOptions.FitWidth = cbFitWidth.Checked;
    
        // set the embedded fonts option - optional - default is false
        pdfConverter.PdfDocumentOptions.EmbedFonts = cbEmbedFonts.Checked;
        // set the live HTTP links option - optional - default is true
        pdfConverter.PdfDocumentOptions.LiveUrlsEnabled = cbLiveLinks.Checked;
    
        // set if the JavaScript is enabled during conversion to a PDF - default
            is true
        pdfConverter.JavaScriptEnabled = cbClientScripts.Checked;
    
        // set if the images in PDF are compressed with JPEG to reduce the
            PDF document size - default is true
        pdfConverter.PdfDocumentOptions.JpegCompressionEnabled = cbJpegCompression.Checked;
    
        // enable auto-generated bookmarks for a specified list of HTML selectors
            (e.g. H1 and H2)
        if (cbBookmarks.Checked)
        {
            pdfConverter.PdfBookmarkOptions.HtmlElementSelectors = new string[] { "H1", "H2" };
        }
    
        // add HTML header
        if (cbAddHeader.Checked)
            AddHeader(pdfConverter);
        // add HTML footer
        if (cbAddFooter.Checked)
            AddFooter(pdfConverter);
    
        // Performs the conversion and get the pdf document bytes that can
    
        // be saved to a file or sent as a browser response
        byte[] pdfBytes = pdfConverter.GetPdfBytesFromUrl(urlToConvert);
    
        // send the PDF document as a response to the browser for download
        System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
        response.Clear();
        response.AddHeader("Content-Type", "application/pdf");
        if (radioAttachment.Checked)
            response.AddHeader("Content-Disposition", 
                    String.Format("attachment; filename=GettingStarted.pdf; size={0}", 
                    pdfBytes.Length.ToString()));
        else
            response.AddHeader("Content-Disposition", 
                    String.Format("inline; filename=GettingStarted.pdf; size={0}", 
                    pdfBytes.Length.ToString()));
        response.BinaryWrite(pdfBytes);
        // Note: it is important to end the response, otherwise the ASP.NET
        // web page will render its content to PDF document stream
        response.End();
    }
    

    【讨论】:

    • 你为什么要给我一个C#的例子?
    猜你喜欢
    • 2010-11-18
    • 2010-12-27
    • 2015-01-18
    • 1970-01-01
    • 2010-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多