PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

can't save to disk AND send to browser ?
https://forum.pdfsharp.net/viewtopic.php?f=2&t=1466
Page 1 of 1

Author:  roro06 [ Wed Dec 08, 2010 11:19 am ]
Post subject:  can't save to disk AND send to browser ?

hi

this is what I have done, as a test :
Code:
 protected PdfDocument genererPDF()
        {
            PdfSharp.Pdf.PdfDocument doc = new PdfSharp.Pdf.PdfDocument();
            string fontName = "Times"; ;
            XFont fontEntete = new XFont(fontName, 35);
            PdfPage page = doc.AddPage();
            page.Width = XUnit.FromMillimeter(210);
            page.Height = XUnit.FromMillimeter(297);
            XGraphics gfx = XGraphics.FromPdfPage(page, XGraphicsUnit.Millimeter);

            gfx.DrawString("TEST", fontEntete, XBrushes.Aqua, 40, 40);
            return doc;
        }

protected void Page_Load(object sender, EventArgs e)
        {
            PdfDocument pdf = genererPDF();
           
            // saving to disk
            string path=@"d:/bid/test.pdf";
            pdf.Save(path);

            // Envoi du pdf au navigateur
            MemoryStream sortie = new MemoryStream();
            pdf.Save(sortie, false);

            Response.Clear();

            Response.ContentType = "application/pdf";
            Response.AddHeader("content-length", sortie.Length.ToString());
            Response.BinaryWrite(sortie.ToArray());
            Response.Flush();
            Response.End();
           }


if i don't save to disk prior to send to browser, it's ok. But if I do, pdf displayed is a blank page, or i get an error popup from acrobat reader. How can I fix this ?

(ps : sorry for my english : i'm french)

Author:  Thomas Hoevel [ Wed Dec 08, 2010 12:34 pm ]
Post subject:  Re: can't save to disk AND send to browser ?

roro06 wrote:
How can I fix this?

Save to MemoryStream only.
Write that MemoryStream to disk (using BinaryWriter.Write(byte[]) Method) and send it to the browser.
Better performance and a workaround for a limitation of PDFsharp.

Or call "genererPDF();" for each Save(). Simple change, but worse performance.

Author:  roro06 [ Thu Dec 09, 2010 11:03 am ]
Post subject:  Re: can't save to disk AND send to browser ?

thanks for your reply.

it works, in this context.

But my problem is a bit more complicated : the page displaying PDFs is a generic one :

pgPdfVisu.aspx :
Code:
 protected void Page_Load(object sender, EventArgs e)
        {
            if Session["pdf"] == null)
            {
                throw new HttpException  (404, "Document inexistant");
            }
            else
            {
                PdfDocument pdf = (PdfDocument)Session["pdf"];

                // Envoi du pdf au navigateur
                MemoryStream sortie = new MemoryStream();
               pdf.Save(sortie, false);
         
                Response.Clear();
     
              Response.ContentType = "application/pdf";
                Response.AddHeader("content-length", sortie.Length.ToString());
                Response.BinaryWrite(sortie.ToArray());
                Response.Flush();
                sortie.Close();
                Response.End();
              }


and pdf are generated (and saved) in several other aspx pages.
eg :
Code:
...
                            PdfDocument pdf = ed.devis_generePDF(true);
                            Session["pdf"] = pdf;

                           // I try to save to disk here

                             ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "editLoad", "var pdf_p=window.open('pgPdfVisu.aspx', 'pdf'); setTimeout(function(){ if(!pdf_p){ window.alert('Un anti-pop-up a empéché d ouvrir le devis');}}, 500);", true);
                       


Author:  Thomas Hoevel [ Tue Dec 14, 2010 4:49 pm ]
Post subject:  Re: can't save to disk AND send to browser ?

roro06 wrote:
But my problem is a bit more complicated

Do you always save the file or only sometimes?

Either case: when you save to file, set Session["pdf"] to null and instead set Session["pdfbytes"] to the array. This requires a slight modification of the generic page.
If you save always, then you always pass the byte[] instead of the PdfDocument.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/