如何使用 iText 7 将 PDF 写入 HttpResponseMessage
How to write PDF to HttpResponseMessage using iText 7
我正在尝试生成 PDF 并使用 iText 7 和 iText7.pdfHtml 库将其写入 HTTP 响应。
PDF 的 HTML 内容存储在 StringBuilder
对象中。
不确定这样做的正确过程是什么,因为一旦我使用 HtmlConverter.ConvertToPdf
,MemoryStream
就会关闭,我无法访问字节。我收到以下异常:
System.ObjectDisposedException: Cannot access a closed Stream.
HttpResponseMessage httpResponseMessage = new HttpResponseMessage();
StringBuilder htmlText = new StringBuilder();
htmlText.Append("<html><body><h1>Hello World!</h1></body></html>");
using (MemoryStream memoryStream = new MemoryStream())
{
using (PdfWriter pdfWriter = new PdfWriter(memoryStream))
{
PdfDocument pdfDocument = new PdfDocument(pdfWriter);
Document document = new Document(pdfDocument);
string headerText = "my header";
string footerText = "my footer";
pdfDocument.AddEventHandler(PdfDocumentEvent.END_PAGE, new HeaderFooterEventHandler(document, headerText, footerText));
HtmlConverter.ConvertToPdf(htmlText.ToString(), pdfWriter);
memoryStream.Flush();
memoryStream.Seek(0, SeekOrigin.Begin);
byte[] bytes = new byte[memoryStream.Length];
memoryStream.Read(bytes, 0, (int)memoryStream.Length);
Stream stream = new MemoryStream(bytes);
httpResponseMessage.Content = new StreamContent(stream);
httpResponseMessage.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/pdf");
httpResponseMessage.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "sample.pdf"
};
httpResponseMessage.StatusCode = HttpStatusCode.OK;
}//end using pdfwriter
}//end using memory stream
编辑
添加了 PdfDocument
和 Document
对象来操作 header/footer 和新页面。
利用你有一个 MemoryStream
并替换
memoryStream.Flush();
memoryStream.Seek(0, SeekOrigin.Begin);
byte[] bytes = new byte[memoryStream.Length];
memoryStream.Read(bytes, 0, (int)memoryStream.Length);
来自
byte[] bytes = memoryStream.ToArray();
该方法被记录为也适用于封闭的内存流。
我正在尝试生成 PDF 并使用 iText 7 和 iText7.pdfHtml 库将其写入 HTTP 响应。
PDF 的 HTML 内容存储在 StringBuilder
对象中。
不确定这样做的正确过程是什么,因为一旦我使用 HtmlConverter.ConvertToPdf
,MemoryStream
就会关闭,我无法访问字节。我收到以下异常:
System.ObjectDisposedException: Cannot access a closed Stream.
HttpResponseMessage httpResponseMessage = new HttpResponseMessage();
StringBuilder htmlText = new StringBuilder();
htmlText.Append("<html><body><h1>Hello World!</h1></body></html>");
using (MemoryStream memoryStream = new MemoryStream())
{
using (PdfWriter pdfWriter = new PdfWriter(memoryStream))
{
PdfDocument pdfDocument = new PdfDocument(pdfWriter);
Document document = new Document(pdfDocument);
string headerText = "my header";
string footerText = "my footer";
pdfDocument.AddEventHandler(PdfDocumentEvent.END_PAGE, new HeaderFooterEventHandler(document, headerText, footerText));
HtmlConverter.ConvertToPdf(htmlText.ToString(), pdfWriter);
memoryStream.Flush();
memoryStream.Seek(0, SeekOrigin.Begin);
byte[] bytes = new byte[memoryStream.Length];
memoryStream.Read(bytes, 0, (int)memoryStream.Length);
Stream stream = new MemoryStream(bytes);
httpResponseMessage.Content = new StreamContent(stream);
httpResponseMessage.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/pdf");
httpResponseMessage.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "sample.pdf"
};
httpResponseMessage.StatusCode = HttpStatusCode.OK;
}//end using pdfwriter
}//end using memory stream
编辑
添加了 PdfDocument
和 Document
对象来操作 header/footer 和新页面。
利用你有一个 MemoryStream
并替换
memoryStream.Flush();
memoryStream.Seek(0, SeekOrigin.Begin);
byte[] bytes = new byte[memoryStream.Length];
memoryStream.Read(bytes, 0, (int)memoryStream.Length);
来自
byte[] bytes = memoryStream.ToArray();
该方法被记录为也适用于封闭的内存流。