将ASP.NET生成的pdf byte []显示到网页而不保存文件

时间:2021-06-25 01:48:21

I'm using iTextSharp for generating a pdf. I can save the PDF file from the PDF byte[].

我正在使用iTextSharp生成pdf。我可以从PDF字节[]中保存PDF文件。

byte[] outputPDF = cnt.CreateBreakPDF();
File.WriteAllBytes(pdfOutPutPath, outputPDF);

What is the best way to display the output byte[] to a web page?

将输出字节[]显示到网页的最佳方法是什么?

I want to show the PDF inside a div in my page. Not the PDF as a full response.

我想在页面中的div中显示PDF。不是PDF作为完整的回复。

I've seen answers for MVC, but I'm using ASP.NET Web Application.

我见过MVC的答案,但我正在使用ASP.NET Web应用程序。

Is there a better way than using HTTP handlers to do so? I don't want to send all the details for creating PDF as query string.

有没有比使用HTTP处理程序更好的方法呢?我不想发送创建PDF作为查询字符串的所有细节。

3 个解决方案

#1


8  

I tried this in jsFiddle, and it works well in Chrome & FF, need to check on other browsers as well.

我在jsFiddle中试过这个,它在Chrome和FF中运行良好,需要检查其他浏览器。

Convert the byte[] to Base64 using,

使用,将byte []转换为Base64,

string base64PDF = System.Convert.ToBase64String(outputPDF, 0, outputPDF.Length);

All I had to do is specify the MIME type as data:application/pdf;base64, in the source and give the Base64 version of the PDF.

我所要做的就是在源代码中将MIME类型指定为data:application / pdf; base64,并提供PDF的Base64版本。

<object data="data:application/pdf;base64, JVBERi0xLjQKJeLjz9MKMyA..." type="application/pdf" width="160px">
    <embed src="data:application/pdf;base64, JVBERi0xLjQKJeLjz9MKMyA..." type="application/pdf" />
</object>

I couldn't be able to hide the top toolbar which appears in FF by appending #toolbar=0&navpanes=0&statusbar=0.

我无法通过附加#toolbar = 0&navpanes = 0&statusbar = 0来隐藏FF中显示的顶部工具栏。

IE8 needs a saved pdf file to be displayed.

IE8需要显示保存的pdf文件。

#2


4  

Try this

Response.ContentType = "application/pdf";
Response.AddHeader("content-length", outputPDF.Length.ToString());
Response.BinaryWrite(outputPDF);

#3


0  

Would something like this work?

这样的东西会起作用吗?

<div>
<object data="myPDF.pdf" type="application/pdf" width="200" height="500">
alt : <a href="myPDF.pdf">myPDF.pdf</a>
</object>
</div> 

You would just need to pass your pdf into the object data source.

您只需将pdf传递到对象数据源即可。

#1


8  

I tried this in jsFiddle, and it works well in Chrome & FF, need to check on other browsers as well.

我在jsFiddle中试过这个,它在Chrome和FF中运行良好,需要检查其他浏览器。

Convert the byte[] to Base64 using,

使用,将byte []转换为Base64,

string base64PDF = System.Convert.ToBase64String(outputPDF, 0, outputPDF.Length);

All I had to do is specify the MIME type as data:application/pdf;base64, in the source and give the Base64 version of the PDF.

我所要做的就是在源代码中将MIME类型指定为data:application / pdf; base64,并提供PDF的Base64版本。

<object data="data:application/pdf;base64, JVBERi0xLjQKJeLjz9MKMyA..." type="application/pdf" width="160px">
    <embed src="data:application/pdf;base64, JVBERi0xLjQKJeLjz9MKMyA..." type="application/pdf" />
</object>

I couldn't be able to hide the top toolbar which appears in FF by appending #toolbar=0&navpanes=0&statusbar=0.

我无法通过附加#toolbar = 0&navpanes = 0&statusbar = 0来隐藏FF中显示的顶部工具栏。

IE8 needs a saved pdf file to be displayed.

IE8需要显示保存的pdf文件。

#2


4  

Try this

Response.ContentType = "application/pdf";
Response.AddHeader("content-length", outputPDF.Length.ToString());
Response.BinaryWrite(outputPDF);

#3


0  

Would something like this work?

这样的东西会起作用吗?

<div>
<object data="myPDF.pdf" type="application/pdf" width="200" height="500">
alt : <a href="myPDF.pdf">myPDF.pdf</a>
</object>
</div> 

You would just need to pass your pdf into the object data source.

您只需将pdf传递到对象数据源即可。