views:

437

answers:

4

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

A: 

Take a peek at print-CSS styles - these are usually also used for HTML to PDF creation. Styles like 'page-break-after' will help you get the page breaks where you need them, so you can create content that is paged (just like you would for the print version of a website).

Hope this helps!

ylebre
+4  A: 

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
}
Alt_Doru
+1  A: 

It sounds doggy for me to generate invoice from existing html. Somehow I feel insecure...

I would generate pdf on server based on data there, but not from html. Check out xsl-fo to pdf components.

Oleg Kalenbet
I agree. It is one of those quick win but messy solutions. Another option to using xsl-fo is iTextSharp-http://itextsharp.sourceforge.net/. To me both are valid approaches depending on your requirements.
RichardOD
We're not "generating invoices from existing HTML"; we're generating HTML output as a representation of the invoice data we have in our records. The HTML is generated by our code server-side on each request - it's just the current way of outputting printer-friendly data. Adding PDF generation on top of it is just an enhancement built on top of existing functionality.
Alt_Doru
Sorry, I got confused by word "existing".
Oleg Kalenbet
A: 

PDF Duo .NET is HTML to PDF converting component for .NET - In contrast to ABC pdf PDF Duo .NET has its own html parsing engine. Here is an example of adding the header & footer:

     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);
     }
Constantine