在Web应用程序中显示PDF

时间:2012-03-10 15:21:04

标签: asp.net pdf web

我用谷歌搜索了这个,搜索了这个,查看了SO和其他网站(我一直试图在这个问题上阅读几个小时),但我仍然无法找到一个令人满意的解决办法似乎是一个简单,常见的编程问题。

让我设置场景:

        
  • 我有一个网站/网络应用程序,允许用户搜索SQL Server库。
  •     
  • 与搜索结果相关的大多数文档都是PDF文件。
  •     
  • PDF文件存储在SQL Server数据库的BLOB中。
  •     
  • 我希望能够从数据库中动态提取PDF并将其显示给用户。
  •     
  • 为了保留用户的搜索进度,我想我想在另一个浏览器窗口/标签页面中打开该文件
  •     
  • 我已经找到了如何将PDF保存到指定目录中的服务器。
  •     
  • 不要希望用户看到该文件的路径。
  •     
  • 在合理范围内,我想要一个适用于所有主流浏览器的解决方案:
  •     
              
    1. Internet Explorer
    2.         
    3. 火狐
    4.         
    5.         
    6. Safari(包括iPhone / iPad Mobile Safari)
    7.          
    8. 我不想购买第三方组件,但如果有必要,我愿意去那条路。
    9.     
    10. 我认为我不想将该文件作为下载文件发送(我想我已经知道该怎么做了),因为这不会在iPhone / iPad上失败吗?

到目前为止我尝试的每个解决方案都有一些基本问题:

        
  • 使用iFrames似乎在iPhone / iPad上惨遭失败
  •     
  • 使用Server.Transfer(我正在使用ASP.NET)显示乱码而不是PDF
  •     
  • 我尝试了几个演示第三方解决方案,但它们也很臭!

我无法理解!我真的是桌面开发人员,在Windows中这是 EASY !为什么网络这么难?

我是愚蠢的,这真是一项简单的练习,还是这项基本任务真的很难?

请帮助我指出正确的方向!

感谢!!!

5 个答案:

答案 0 :(得分:7)

您是否尝试使用content-disposition-inline添加http标头?您也可能希望将结果直接写入输出响应而不是保存它。这将确保文件实际路径不会显示为您直接将其写入响应。

例如

Response.ContentType = "application/pdf";
Response.AddHeader("Content-disposition","inline");
Response.BinaryWrite(myfilestream.ToArray());

如果myfilestream是一个内存流,或者如果你已经有一个blob的字节数组,你可以直接将它写入响应而不用toarray

答案 1 :(得分:5)

此链接可能对您有用,

http://nilangshah.wordpress.com/2007/05/28/successfully-stream-a-pdf-to-browser-through-https/

您可以通过为链接指定target =“_ blank”,在新标签页中打开pdf。博客中提到的ByteArray是来自DB的BLOB。希望这会有所帮助。

答案 2 :(得分:1)

ASP.Net有一个ReportViewer服务器控件,可用于显示PDF文件。

有关此功能的大部分文档都是关于如何生成报告并将其导出为PDF。 MSDN真的不是很有帮助。我想每个人都依赖Adobe Reader而不是寻找替代方案?

但也可以导入并显示PDF 。此代码似乎适用于this user

public static void RenderToPdf(ReportViewer reportViewer, Boolean forceDownload)
{
    string path = (string.IsNullOrEmpty(reportViewer.LocalReport.ReportPath)) ? reportViewer.ServerReport.ReportPath : reportViewer.LocalReport.ReportPath;
    RenderToPdf(reportViewer, forceDownload, System.IO.Path.GetFileNameWithoutExtension(path));
}

public static void RenderToPdf(ReportViewer reportViewer, Boolean forceDownload, string fileNameWithoutExtension)
{
    HttpContext context = HttpContext.Current;

    if (!context.Response.Buffer)
    {
        return; //can not clear the buffer, so exit
    }

    //define out properties
    Warning[] warnings;
    string mimeType, encoding, fileNameExtension;
    string[] streams;

    //get pdf content
    Byte[] pdfContent = reportViewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out fileNameExtension, out streams, out warnings);

    //cancel and clear the existing output!
    context.Response.Clear();
    context.Response.ContentType = "application/pdf";

    //add a header so that the user can save the target as a downloaded file
    if (forceDownload)
    {
        context.Response.AddHeader("Content-disposition", string.Format("attachment; filename={0}.pdf", fileNameWithoutExtension));
    }

    context.Response.BinaryWrite(pdfContent);
    context.Response.End();
}

答案 3 :(得分:0)

这是我在ASP.NET(aspx)页面中在浏览器中打开PDF文档的方法。 在OnLoad页面中:

        this.Response.Clear();
        this.Response.Buffer = true;
        this.Response.ContentType = "application/pdf";
        this.Response.AddHeader("content-length", pdfReportStream.Length.ToString());
        this.Response.BinaryWrite(pdfReportStream.ToByteArray());

        this.Response.End();

答案 4 :(得分:0)

如果您正在寻找客户端pdf客户端,您可以查看PDF.js,它可以显示包含默认查看器的任何PDF(您必须转换viewer.html进入视图,如果在MVC,但它很容易)。 它是开源和活着的。我必须通过评论js中的一些行来稍微调整它以便能够在iphone(数字签名)上显示pdf注释。

相关问题