Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate PDF - how to create paged HTML content beforehand?

My task is to create ready-to-print invoices from a .NET web app.

I am already generating the printer-friendly HTML for an invoice - it consists of an invoice header (that will actually need to appear on each printed page) and invoice positions (that may span multiple printed pages).

Now, my task is to generate a PDF server-side, populate a header on each of its pages, then populate its pages with invoice positions. I need to generate the PDF from the existing HTML data - by simply passing HTML input to the PDF generator library.

I've started using ABCPDF - I am using the AddImageHtml method. The problem I have is that ABCPDF seems to expect me to supply HTML content already paged. So, it won't work correctly when I feed it HTML content that would span on more than 1 PDF page.

So, my question is - do you have any suggestions on making this work with ABCPDF? Or, more generally speaking, what other tools/approaches would you use for this - generating PDF doc with headers/footers from HTML input?

Thanks

like image 266
Alt_Doru Avatar asked Jan 22 '26 21:01

Alt_Doru


1 Answers

Found an answer for my ABCPDF-specific question. I am now passing the contents of my generated HTML file as a string to the method below:

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
}
like image 107
Alt_Doru Avatar answered Jan 25 '26 17:01

Alt_Doru