生成PDF - 如何预先创建分页HTML内容?

时间:2009-07-07 06:30:25

标签: .net asp.net pdf-generation

我的任务是从.NET Web应用程序创建可立即打印的发票。

我已经为发票生成了打印机友好的HTML - 它包含发票标题(实际上需要在每个打印页面上显示)和发票位置(可能跨越多个打印页面)。

现在,我的任务是生成PDF服务器端,在每个页面上填充标题,然后使用发票位置填充其页面。我需要从现有的HTML数据生成PDF - 只需将HTML输入传递给PDF生成器库。

我开始使用ABCPDF - 我正在使用AddImageHtml方法。我遇到的问题是ABCPDF似乎希望我提供HTML内容已经分页。因此,当我提供跨越1个PDF页面的HTML内容时,它将无法正常工作。

所以,我的问题是 - 你对ABCPDF的这项工作有什么建议吗?或者,更一般地说,您将使用哪些其他工具/方法 - 从HTML输入生成带有页眉/页脚的PDF文档?

由于

4 个答案:

答案 0 :(得分:4)

找到了我的ABCPDF特定问题的答案。我现在将生成的HTML文件的内容作为字符串传递给下面的方法:

private void GeneratePdf(string output)
{
    var theDoc = new Doc();
    theDoc.Rect.Inset(72, 144);

    int theID = theDoc.AddImageHtml(output, true, 800, true);

    while (true)
    {
        theDoc.FrameRect();
        if (!theDoc.Chainable(theID))
            break;
        theDoc.Page = theDoc.AddPage();
        theID = theDoc.AddImageToChain(theID);
    } 

    string fileLocation = Server.MapPath("testAbcPdf.pdf");
    theDoc.Save(fileLocation);
   // send file to browser as downloadable PDF here
}

答案 1 :(得分:1)

从现有的HTML生成发票对我来说听起来很小便。不知怎的,我感到不安全......

我会根据数据在服务器上生成pdf,但不会从html生成。 查看xsl-fo到pdf组件。

答案 2 :(得分:0)

查看print-CSS样式 - 这些样式通常也用于HTML到PDF的创建。像'page-break-after'这样的样式将帮助您在需要的地方获得分页符,这样您就可以创建被分页的内容(就像您对网站的打印版本一样)。

希望这有帮助!

答案 3 :(得分:0)

PDF Duo .NET is HTML to PDF converting component for .NET - 与ABC pdf相比,PDF Duo .NET有自己的html解析引擎。这是添加标题和放大器的示例。页脚:

     string string _ html = @"Or you can point here the html file. Put your image <img src='pic.jpg' widtd='140' height='70'>";
     string file _ pdf = @"K:\new.pdf";

     try
     {
         DuoDimension.HtmlToPdf conv = new DuoDimension.HtmlToPdf();
         conv.Header = "Invoce Document";
         conv.Footer = "* If payment made by Bank transfer kindy please forward to us the copy of the bank transfer slip";
         conv.OpenHTML(file _ html);
         conv.SavePDF(file _ pdf);
         conv.ShowPDF(file _ pdf, Response)
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }