Why is MigraDoc generating a blank pdf in my asp.net application?

自作多情 提交于 2019-12-24 17:15:25

问题


I have the following code in my attempts to create a PDF:

    public static MemoryStream Test()
    {
        var document = new Document();
        document.Info.Title = "Test Report";
        document.Info.Subject = "blah";
        document.Info.Author = "Me";
        //new CoverPageSummarySection().AddToDocument(document, new int[0], 2004);

        Style style = document.Styles["Normal"];
        style.Font.Name = "Times New Roman";
        style = document.Styles["Heading1"];
        style.Font.Name = "Tahoma";
        style.Font.Size = 14;
        style.Font.Bold = true;
        style.Font.Color = Colors.DarkBlue;
        style.ParagraphFormat.PageBreakBefore = true;
        style.ParagraphFormat.SpaceAfter = 6;

        var section = document.AddSection();
        var p = section.AddParagraph("test");
        p.AddText("Testing 1234");

        var renderer = new PdfDocumentRenderer(true, PdfSharp.Pdf.PdfFontEmbedding.Always);
        renderer.Document = document;
        renderer.RenderDocument();

        var ms = new MemoryStream();
        renderer.PdfDocument.Save(ms, false);
        return ms;
    }

The resulting pdf is blank. I can view the properties and the document.Info fields are showing correctly in my PDF, but I can't see any text on my page.

What am I doing wrong?


Edit: So it appears that the issue has something to do with saving to a memory stream. When I replace renderer.PdfDocument.Save(ms, false); to renderer.PdfDocument.Save("e:\\test.pdf"); it saves it correctly at test.pdf.

My code to save the memory stream to the asp.net output is:

        var stream = TestReportGen.Test();

        // Set the content headers
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.Buffer = true;
        HttpContext.Current.Response.ContentType = "application/pdf";
        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=testReport.pdf");

        stream.WriteTo(HttpContext.Current.Response.OutputStream);
        stream.Close();

        HttpContext.Current.Response.End();

Is the issue with how I'm sending back the memorystream or what?


回答1:


Assuming you have a valid MigraDoc document, the following should work:

PdfDocumentRenderer renderer = new PdfDocumentRenderer(true, PdfFontEmbedding.Always);
renderer.Document = document;
renderer.RenderDocument();

// Send PDF to browser
MemoryStream stream = new MemoryStream();
renderer.PdfDocument.Save(stream, false);
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", stream.Length.ToString());
Response.BinaryWrite(stream.ToArray());
Response.Flush();
stream.Close();
Response.End();



回答2:


The majority of my problems with streams are solved by Seek(0, SeekOrigin.Begin).




回答3:


I am not an ASP.NET expert, but there is a working web server sample included with PDFsharp:
http://www.pdfsharp.net/wiki/Clock-sample.ashx

I don't know whether it's the missing content-length that makes the difference.

Seek(0, SeekOrigin.Begin) was missing with early versions of PDFsharp, but is done automatically in the current version.



来源:https://stackoverflow.com/questions/12864051/why-is-migradoc-generating-a-blank-pdf-in-my-asp-net-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!