如何从HTTP流汇编pdf?

时间:2013-08-04 11:48:54

标签: c# asp.net http pdf-generation

我正在使用第三方HTML转换为pdf(DocRaptor)。您将HTML发布到他们的网站,然后他们回复PDF。他们给你的入门代码工作正常,但它把文件放到你的硬盘上。我修改了他们的代码,让它通过浏览器和文件下载。所以我100%确信我从HTTP响应获得的数据是好数据。我似乎无法将它组装回可用的文件。

我有理由相信问题是我如何处理responseStream数据。一旦我进入Try / Catch,这一切似乎都出错了。我是c#和网络编程的新手,所以我非常感谢SO用户的一些指导。谢谢。这是我的代码。

            string postData = String.Format(PostFormat,
            (string.IsNullOrEmpty(DocumentContent) ? "document_url" : "document_content"),
            HttpUtility.UrlEncode(string.IsNullOrEmpty(DocumentContent) ? DocumentURL : DocumentContent),
            HttpUtility.UrlEncode(Name),
            HttpUtility.UrlEncode(type),
            HttpUtility.UrlEncode(Test.ToString().ToLower()),
            HttpUtility.UrlEncode(Strict),
            HttpUtility.UrlEncode(PrinceOptions));

        var byteArray = Encoding.UTF8.GetBytes(postData);

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(DocRaptorUrl);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = byteArray.Length;
        using (var dataStream = request.GetRequestStream()) { dataStream.Write(byteArray, 0, byteArray.Length); }

        System.IO.Stream stream = null;

        try 
        {
            using (HttpWebResponse httpResponse = (HttpWebResponse)request.GetResponse())
            {
                using (System.IO.Stream responseStream = httpResponse.GetResponseStream())
                {

                    var filepath = @"C:\Users\David\Downloads\UberwriterUSRReport.pdf";
                    HttpContext.Current.Response.ContentType = "application/pdf";

                    // let the browser know how to open the PDF document, attachment or inline, and the file name
                    HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("attachment; filename=UberwriterUSRReport.pdf"));

                    stream = new System.IO.FileStream(filepath, System.IO.FileMode.Create);

                    CopyStream(responseStream, stream);

                    long bytestToRead = stream.Length;

                    while (bytestToRead > 0)
                    {
                        if (HttpContext.Current.Response.IsClientConnected)
                        {
                            byte[] buffer = new Byte[10000];  
                            int length = stream.Read(buffer, 0, 10000);
                            HttpContext.Current.Response.OutputStream.Write(buffer, 0, length);
                            HttpContext.Current.Response.Flush();

                            bytestToRead = bytestToRead - length;
                        }
                        else
                        {
                            bytestToRead = -1;
                        }
                    }
                }
            }
        }

2 个答案:

答案 0 :(得分:3)

您是否打算在将文件发送到浏览器之前将其保存到硬盘?因为这是你现在(错误地)做的事情。

最好将write动作括在using语句中,因为我没有看到你在任何地方关闭流:

stream = new System.IO.FileStream(filepath, System.IO.FileMode.Create);

在这里你保存到文件:

CopyStream(responseStream, stream);

接下来,您尝试读取输出流(您刚刚保存文件),将其写入Response.Outputstream。你已经完成了一个拷贝流实现,为什么要在这里手动完成呢? :

HttpContext.Current.Response.OutputStream.Write(buffer, 0, length);

所以,我会说它应该是这样的:

using (HttpWebResponse httpResponse = (HttpWebResponse)request.GetResponse())
{
    using (System.IO.Stream responseStream = httpResponse.GetResponseStream())
    {
        var filepath = @"C:\Users\David\Downloads\UberwriterUSRReport.pdf";
        HttpContext.Current.Response.ContentType = "application/pdf";

        // let the browser know how to open the PDF document, attachment or inline, and the file name
        HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("attachment; filename=UberwriterUSRReport.pdf"));

        using (var stream = new System.IO.FileStream(filepath, System.IO.FileMode.Create)) {
          CopyStream(responseStream, stream);
        }

        using (var readstream = new System.IO.FileStream(filepath, System.IO.FileMode.Read)) {
            CopyStream(readstream, HttpContext.Current.Response.OutputStream);
        }
    }
}

或者,如果您根本不想将文件保存在服务器上:

using (HttpWebResponse httpResponse = (HttpWebResponse)request.GetResponse())
{
    using (System.IO.Stream responseStream = httpResponse.GetResponseStream())
    {
        // let the browser know how to open the PDF document, attachment or inline, and the file name
        HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("attachment; filename=UberwriterUSRReport.pdf"));

      CopyStream(responseStream, HttpContext.Current.Response.OutputStream);
    }
}

答案 1 :(得分:1)

很多人非常感谢斯蒂芬让我走上了正确的道路。我进一步完善了实施。我有更多的代码需要什么。我想要的只是用户点按一下按钮,将HTML发布到DocRaptor.com网站,让他们用生成的PDF回复,然后该文件在浏览器中显示为下载。以下是在Azure上测试的最终实现代码。

            try 
        {
            using (HttpWebResponse httpResponse = (HttpWebResponse)request.GetResponse())
            {
                using (System.IO.Stream responseStream = httpResponse.GetResponseStream())
                {

                    //var filepath = @"C:\Users\David\Downloads\UberwriterUSRReport.pdf";
                    HttpContext.Current.Response.Clear();
                    HttpContext.Current.Response.ContentType = "application/pdf";
                    HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("atachment; filename=UberwriterUSRReport.pdf"));
                    HttpContext.Current.Response.BufferOutput = true;

                    CopyStream(responseStream, HttpContext.Current.Response.OutputStream);

                }
            }
        }