hiqpdf-html转换问题



我正在使用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转换的页面显示为空页面,只有谷歌chrome才能正确显示它们,在firefox中,这些页面在加载状态下无限期地继续。

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

任何帮助都将不胜感激!!thx!!

是的,在关闭主文档之前,添加到主文档中的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();
}

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

相关内容

  • 没有找到相关文章

最新更新