简体   繁体   English

为什么 MigraDoc 在我的 asp.net 应用程序中生成空白 pdf?

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

I have the following code in my attempts to create a PDF:我在尝试创建 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.生成的pdf是空白的。 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.我可以查看属性和document.Info字段在我的 PDF 中正确显示,但我在我的页面上看不到任何文本。

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); 当我替换renderer.PdfDocument.Save(ms, false); to renderer.PdfDocument.Save("e:\\\\test.pdf"); renderer.PdfDocument.Save("e:\\\\test.pdf"); it saves it correctly at test.pdf. 它正确地保存在 test.pdf 中。

My code to save the memory stream to the asp.net output is:我将内存流保存到 asp.net 输出的代码是:

 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?是我如何发回内存流的问题还是什么?

Assuming you have a valid MigraDoc document, the following should work:假设您有一个有效的 MigraDoc 文档,以下应该可以工作:

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();

我的大部分流问题都由Seek(0, SeekOrigin.Begin)

I am not an ASP.NET expert, but there is a working web server sample included with PDFsharp:我不是 ASP.NET 专家,但 PDFsharp 中包含一个可用的 Web 服务器示例:
http://www.pdfsharp.net/wiki/Clock-sample.ashx http://www.pdfsharp.net/wiki/Clock-sample.ashx

I don't know whether it's the missing content-length that makes the difference.我不知道是否是缺少content-length造成了差异。

Seek(0, SeekOrigin.Begin) was missing with early versions of PDFsharp, but is done automatically in the current version.早期版本的 PDFsharp 中缺少Seek(0, SeekOrigin.Begin) ,但在当前版本中会自动完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM