Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
Hi all,
 
My task is to generate the invoice in pdf. I have an html template. i am replacing some of the html content with name, address, contact number into a string . Now i am unable to get the pdf by using the itextsharper. I am not getting the css, perfect design in the pdf. pdf is generating by using itextsharper. Can anyone help me.
Posted 4-Oct-12 1:31am
Comments
Ashraff Ali Wahab at 4-Oct-12 10:13am
   
All CSS are not coming or you have problem with specific CSS or images.
If possible post the sample HTML and code you used to generate the PDF

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Code:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iTextSharp.text;
using iTextSharp.text.html;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
using System.IO;
using System.util;
using System.Net;
using System.Xml;
 
protected void btn_submit_Click(object sender, EventArgs e)
    {
        using (StreamReader reader = new StreamReader(Server.MapPath("~") + "/App_Data/test.html"))
        {
            String line = reader.ReadToEnd();
            Text2PDF(line);
        }
    }
 
    protected void Text2PDF(string PDFText)
    {
        //HttpContext context = HttpContext.Current;
        StringReader reader = new StringReader(PDFText);
 
        //Create PDF document 
        Document document = new Document(PageSize.A4);
        HTMLWorker parser = new HTMLWorker(document);
 
        string PDF_FileName = Server.MapPath("~") + "/App_Data/PDF_File.pdf";
        PdfWriter.GetInstance(document, new FileStream(PDF_FileName, FileMode.Create));
        document.Open();
 
        try
        {
            parser.Parse(reader);
        }
        catch (Exception ex)
        {
            //Display parser errors in PDF. 
            Paragraph paragraph = new Paragraph("Error!" + ex.Message);
            Chunk text = paragraph.Chunks[0] as Chunk;
            if (text != null)
            {
                text.Font.Color = BaseColor.RED;
            }
            document.Add(paragraph);
        }
        finally
        {
            document.Close();
            DownLoadPdf(PDF_FileName);
        }
    }
 
    private void DownLoadPdf(string PDF_FileName)
    {
        WebClient client = new WebClient();
        Byte[] buffer = client.DownloadData(PDF_FileName);
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-length", buffer.Length.ToString());
        Response.BinaryWrite(buffer);
    }
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 335
1 Gihan Liyanage 332
2 OriginalGriff 236
3 ClimerChinna 222
4 vikinghunter 168
0 Sergey Alexandrovich Kryukov 8,243
1 OriginalGriff 7,109
2 CPallini 2,598
3 Richard MacCutchan 1,980
4 Abhinav S 1,778


Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 4 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100