【问题标题】:hiqpdf html conversion issuehiqpdf html转换问题
【发布时间】:2015-05-26 16:15:45
【问题描述】:

我正在使用 HiQPdf 将 html 页面列表转换并合并为一个 pdf 文档。 我就是这样做的:

public class HtmlToPdfEditor
{
    private string _firstPage;
    private string _secondPage;
    //private const string _HiQPdfSerialNumber = "";
    private PdfDocument _document;
    public HtmlToPdfEditor(string firstPage, string secondPage)
    {
        _firstPage = firstPage;
        _secondPage=secondPage;
    }

    public void ConvertAll(string outputPath) 
    {
         HtmlToPdf htmlToPdfConverter = new HtmlToPdf();
        _document = new PdfDocument();
        //_document.SerialNumber = _HiQPdfSerialNumber;
        string firstPageDoc = GetDocument(_firstPage, "firstPage.pdf");
        string secondPageDoc = GetDocument(_secondPage, "secondtPage.pdf");

        this.JoinDocument(PdfFromFile(firstPageDoc));
        this.JoinDocument(PdfFromFile(secondPageDoc));
        _document.WriteToFile(outputPath);
        _document.Close();
        _document = null;
    }

    private PdfDocument PdfFromFile(string path)
    {
        return PdfDocument.FromFile(path);
    }


    private int JoinDocument(PdfDocument document)
    {
        var nbPages = _document.Pages.Count;
        _document.AddDocument(document);
        document.Close();
        return nbPages;
    }

    private string GetDocument(string content, string outputFile)
    {
       var baseUrl = "";
       var htmlToPdfConverter = GetPdfExporter();
       htmlToPdfConverter.ConvertHtmlToFile(content, baseUrl, outputFile);
       return outputFile;
    }

    public HtmlToPdf GetPdfExporter()
    {
        HtmlToPdf htmlToPdfConverter = new HtmlToPdf();
        //htmlToPdfConverter.SerialNumber = _HiQPdfSerialNumber;
        htmlToPdfConverter.Document.PageSize = PdfPageSize.A4;
        htmlToPdfConverter.Document.PageOrientation = PdfPageOrientation.Portrait;
        htmlToPdfConverter.Document.Margins = new PdfMargins(2);
        htmlToPdfConverter.HtmlLoadedTimeout = 60;
        htmlToPdfConverter.TriggerMode = ConversionTriggerMode.WaitTime; //Time to load the html
        htmlToPdfConverter.WaitBeforeConvert = 1;
        return htmlToPdfConverter;

    }
}

这里的问题是,在生成的文档中,从 html 转换的页面显示为空页面,只有 google chrome 才能正确显示它们,在 Firefox 中这些页面无限期地处于加载状态。

请注意,如果我将 Html 转换为 PdfDocument 而不是将其存储到文件中然后加入它。生成的文档完全可读,但不幸的是我不能使用这种方法。

任何帮助将不胜感激!谢谢!!

【问题讨论】:

    标签: c# html pdf-generation html-to-pdf hiqpdf


    【解决方案1】:

    是的,没错,您添加到主文档的 PDF 文档必须保持打开状态,直到您关闭主文档。

    如果您合并的 PDF 文档是从 HTML 生成的,那么实际上有一种更简单的方法可以按照 Convert Many HTML to PDF 示例中的方法将 HTML 文档合并到 PDF 中。

    // create an empty PDF document
    PdfDocument document = new PdfDocument();
    
    // add a page to document
    PdfPage page1 = document.AddPage(PdfPageSize.A4, new PdfDocumentMargins(5), 
            PdfPageOrientation.Portrait);
    
    try
    {
        // set the document header and footer before 
        // adding any objects to document
        SetHeader(document);
        SetFooter(document);
    
        // layout the HTML from URL 1
        PdfHtml html1 = new PdfHtml(textBoxUrl1.Text);
        PdfLayoutInfo html1LayoutInfo = page1.Layout(html1);
    
        // determine the PDF page where to add URL 2
        PdfPage page2 = null;
        System.Drawing.PointF location2 = System.Drawing.PointF.Empty;
        if (checkBoxNewPage.Checked)
        {
            // URL 2 is laid out on a new page with the selected orientation
            page2 = document.AddPage(PdfPageSize.A4, new PdfDocumentMargins(5), 
                GetSelectedPageOrientation());
            location2 = System.Drawing.PointF.Empty;
        }
        else
        {
            // URL 2 is laid out immediately after URL 1 and html1LayoutInfo
            // gives the location where the URL 1 layout finished
            page2 = document.Pages[html1LayoutInfo.LastPageIndex];
            location2 = new System.Drawing.PointF(html1LayoutInfo.LastPageRectangle.X, 
                html1LayoutInfo.LastPageRectangle.Bottom);
        }
    
        // layout the HTML from URL 2
        PdfHtml html2 = new PdfHtml(location2.X, location2.Y, textBoxUrl2.Text);
        page2.Layout(html2);
    
        // write the PDF document to a memory buffer
        byte[] pdfBuffer = document.WriteToMemory();
    
        // inform the browser about the binary data format
        HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf");
    
        // let the browser know how to open the PDF document
        HttpContext.Current.Response.AddHeader("Content-Disposition", 
                    String.Format("attachment; filename=LayoutMultipleHtml.pdf;
                            size={0}",
                    pdfBuffer.Length.ToString()));
    
        // write the PDF buffer to HTTP response
        HttpContext.Current.Response.BinaryWrite(pdfBuffer);
    
        // call End() method of HTTP response 
        // to stop ASP.NET page processing
        HttpContext.Current.Response.End();
    }
    finally
    {
        document.Close();
    }
    

    【讨论】:

      【解决方案2】:

      好的,我通过确保在关闭最终文档后关闭所有要合并的 pdf 文档来解决此问题。换句话说,JoinDocument 方法将不再调用 document.Close()。我会在关闭最终文档(_document)后调用它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-05-05
        • 1970-01-01
        • 2021-10-08
        • 2011-11-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-04
        • 1970-01-01
        相关资源
        最近更新 更多