开发者

Creation of PDF file using itextSharp

开发者 https://www.devze.com 2023-01-31 16:01 出处:网络
Hi I am creating an ASP.net website where some articles can be uploaded. I need that my users can get PDF version of it by c开发者_高级运维licking on a button. For this functionality I am using itext

Hi I am creating an ASP.net website where some articles can be uploaded. I need that my users can get PDF version of it by c开发者_高级运维licking on a button. For this functionality I am using itextSharp. I am considering the following two options. 1. I will create the PDF file once(on first request) and reuse it always by checking for the existence of it. 2. I will create it on the fly and delete it as soon as the PDF file is delivered to the client. The first approach will lead to faster PDF delivery where as the second approach will lead to saving space. Can anyone please tell me which of the options is better?

I am also wondering if this can be possible without saving the PDF on the server in the first place. So can anyone reply me as soon as possible.

Thanks Dipa


So your tradeoff is saving CPU cycles by storing the file versus saving disk space by always generating it. I would recommend saving CPU cycles because adding disk storage is cheap and easy (can be done without taking the server offline). Adding CPUs is relatively more expensive and usually requires taking the server offline (depends on your environment).

Option 1: Create the PDF on first request

Advantages:

  • PDF is not created unless someone downloads it, so no wasted CPU on creating PDFs nobody wants.
  • PDF is a file, so IIS can transmit it to the client as a file, it is available for IIS caching strategies, etc.

Disadvantages:

  • PDF file takes up space on disk.
  • Need to mark the PDF (or delete it) if the content is changed, otherwise the PDF version could be out of sync with the article.

Option 2: Create the PDF on every request

Advantages:

  • PDF is always up-to-date with the latest version of the article.
  • Disk space is minimized.

Disadvantages:

  • Heavy CPU load on the server if many users request a PDF download.
  • Unable to use IIS or other web server caching strategies.


You can definitely create the PDF on the fly without writing it to disk. Instead of using a FileStream use a MemoryString:

        'PDF Document'
        Dim document As New Document(PageSize.LETTER)
        'Use a memory string so we don't need to write to disk
        Using outputStream As New MemoryStream()
            'Associate the PDF with the stream
            Dim w = PdfWriter.GetInstance(document, outputStream)

            'Open the PDF for writing'
            document.Open()

            'Do PDF stuff Here'


            'Close the PDF'
            document.Close()
            'Clear the response buffer'
            Response.Clear()
            'Set the output type as a PDF'
            Response.ContentType = "application/pdf"
            'Disable caching'
            Response.AddHeader("Expires", "0")
            Response.AddHeader("Cache-Control", "")
            'Set the filename'
            Response.AddHeader("Content-Disposition", "attachment; filename=" & OutputFileName)
            'Set the length of the file so the browser can display an accurate progress bar'
            Response.AddHeader("Content-length", outputStream.GetBuffer().Length.ToString())
            'Write the contents of the memory stream'
            Response.OutputStream.Write(outputStream.GetBuffer(), 0, outputStream.GetBuffer().Length)
            'Close the response stream'
            Response.End()

        End Using

iTextSharp is very, very fast for me. I'm on a shared host and I can generate fairly complex PDFs of dozens of pages without noticing any sluggishness.

EDIT Here's the code converted using the VB.Net to C# converter. I haven't tested it and you might have to clean up a couple of things but it should be pretty straight forward.

Document document = new Document(PageSize.LETTER);
//Use a memory string so we don't need to write to disk
using (MemoryStream outputStream = new MemoryStream()) {
    //Associate the PDF with the stream
    dynamic w = PdfWriter.GetInstance(document, outputStream);

    //Open the PDF for writing'
    document.Open();

    //Do PDF stuff Here'


    //Close the PDF'
    document.Close();
    //Clear the response buffer'
    Response.Clear();
    //Set the output type as a PDF'
    Response.ContentType = "application/pdf";
    //Disable caching'
    Response.AddHeader("Expires", "0");
    Response.AddHeader("Cache-Control", "");
    //Set the filename'
    Response.AddHeader("Content-Disposition", "attachment; filename=" + OutputFileName);
    //Set the length of the file so the browser can display an accurate progress bar'
    Response.AddHeader("Content-length", outputStream.GetBuffer().Length.ToString());
    //Write the contents of the memory stream'
    Response.OutputStream.Write(outputStream.GetBuffer(), 0, outputStream.GetBuffer().Length);
    //Close the response stream'
    Response.End();

}

To open a PDF in a new window have your link button point to a page such as "CreatePDF.aspx". That page should have this code in it.


Hi Dipa As DarrellNoton said it's important to save CPU cycles, i am also agreeing to that. No one will wait for a long time to get the PDF ready, the end user will want it faster.

I will suggest you to create it once and store it in DB as binary format. So that you can access it quickly and it will take less time than creating a new PDF everytime. Or you can give a name to the PDf file and store the name only in DB. the name can be a guid.

Hope this will help you. Thanks :)


If you are using iTextSharp it can't be possible to create it on the fly. So the second option seems to be good. If you want to reduce the server space used you can run some windows service which will delete all / the less used PDFs from the server.

It can solve both of your problem i think.


Actually its totally depends on application requirement.Like in my application I don't need to save pdf file on server so after pdf file delivered to client side i delete it from the server. So in your case if particular user access particular pdf file multiple times then you have to save it on the server,it would save your pdf generation time.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号