0

我正在使用 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 而不是将其存储到文件中然后加入它。生成的文档是完全可读的,但不幸的是我不能使用这种方法。

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

4

2 回答 2

1

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

如果您合并的 PDF 文档是从 HTML 生成的,那么实际上有一种更简单的方法可以按照将多个 HTML转换为 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();
}
于 2016-02-16T09:23:43.833 回答
0

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

于 2015-03-24T08:05:28.000 回答