首页 新闻 搜索 专区 学院

C#中接收到到网页传回的pdf文件流,如何转化为pdf文件?

0
悬赏园豆:20 [已解决问题] 解决于 2014-12-02 22:16

网页直接返回的是一个pdf的文件流,使用ITextSharp也无法转换为想要的pdf格式。

问题补充:

返回的pdf文件流是完整的,已经包含了%pdf1.4等字样~

换个问题,需要使用C#实现类似Java以下功能的代码:

HttpClient httpclient = new DefaultHttpClient();
HttpPost post=new HttpPost("https://www.xxx.com/");
String xml=IOUtils.toString(new FileInputStream(new File("src","/test/conf/export.xml")));
post.setEntity(new StringEntity(xml,HTTP.UTF_8));
HttpResponse response = httpclient.execute(post);
FileOutputStream fOut = new FileOutputStream("d://export.pdf");
response.getEntity().writeTo(fOut);
啸飛同学的主页 啸飛同学 | 初学一级 | 园豆:133
提问于:2014-12-02 13:01
< >
分享
最佳答案
0

Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=filename.pdf"); 

?

收获园豆:20
Yu | 专家六级 |园豆:12944 | 2014-12-02 14:45

byte[] bs = Encoding.UTF8.GetBytes(para);
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
req.Method = questMethod;
req.ContentType = "application/pdf";
req.ContentLength = bs.Length;

// 返回的流数据信息
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(bs, 0, bs.Length);
}

using (WebResponse wr = req.GetResponse())
{
StreamReader responseStream = new StreamReader(wr.GetResponseStream(), Encoding.UTF8);
strResponse = responseStream.ReadToEnd();
}

这样设置了还是不得行~

啸飛同学 | 园豆:133 (初学一级) | 2014-12-02 15:09

取回来的数据样式是这样的~

%PDF-1.4
%����
4 0 obj
<<
/Creator (�Z�}��G�ӳ��l�G��l��~�_D�\\o��>�����$�����%)
/Producer (�Z�}��G�ӳ��l�G��l��~�_D�\\o��>�����$�����%)
/CreationDate (�Z�x��G����=�V��l���ND�\no��y����H�<�����$D)
>>
endobj
6 0 obj
<<
/N 3
/Length 12 0 R
/Filter /FlateDecode
>>
stream
�e����x~���w�A��ۦ��l��4��

啸飛同学 | 园豆:133 (初学一级) | 2014-12-02 15:36

@啸飛同学: 那你咋么保存的?

Yu | 园豆:12944 (专家六级) | 2014-12-02 16:35

@Yu:前面的方法会等到一个strResponse,然后用ITextSharp创建了个文件保存的~

ViewMode
1 FileStream fileStream = new FileStream(string.Format("{0}.pdf", DateTime.Now.ToString("yyyyMMddHHmmss")), FileMode.Create);
2 Document document = new Document();
3 PdfWriter.GetInstance(document, fileStream); 
4 document.Open();
5 document.Add(new Paragraph(strResponse));
6 document.Close();
啸飛同学 | 园豆:133 (初学一级) | 2014-12-02 17:08

@啸飛同学: 

 using (MemoryStream memoryStream = new MemoryStream())
                        {
                                Document doc = new Document();
                                PdfWriter myPDFWriter = PdfWriter.GetInstance(doc, memoryStream);

                                doc.Open();
                                doc.Add(new Paragraph(strResponse));

                                doc.Close();

                                byte[] content = memoryStream.ToArray();

                                using (FileStream fs = File.Create("test.pdf"))
                                {
                                        fs.Write(content, 0, (int)content.Length);
                                }
                        }

试试

Yu | 园豆:12944 (专家六级) | 2014-12-02 17:37

@Yu: 还是一样的~

%PDF-1.4
%
4 0 obj
<<
/Creator (4{ˇ5 38OBb+^•\\EN)
/Producer (4{ˇ5 38OBb+^•\\EN)
/CreationDate (4{ˇ•s3I8i0B\f+•D[N)
>>
endobj
6 0 obj
<<

页面有提供个Demo 但是是Java的,就是想找个C#的方式

HttpClient httpclient = new DefaultHttpClient();
HttpPost post=new HttpPost("XXXXXXXX");
String xml=IOUtils.toString(new FileInputStream(new File("src","/test/java/com/jzspark/biz/conf/export.xml")));
post.setEntity(new StringEntity(xml,HTTP.UTF_8));
HttpResponse response = httpclient.execute(post);
FileOutputStream fOut = new FileOutputStream("d://export.pdf");//导出PDF使用此处2行代码,并注释输出的代码,其他功能注释
response.getEntity().writeTo(fOut);
View Code

 

啸飛同学 | 园豆:133 (初学一级) | 2014-12-02 18:05

@啸飛同学: 

  using (WebResponse wr = req.GetResponse())
                {
                    Stream stream = wr.GetResponseStream();
                    int length = (int)wr.ContentLength;
                    BinaryReader br = new BinaryReader(stream);
                    FileStream fs = File.Create("export.pdf");
                    fs.Write(br.ReadBytes(length), 0, length);
                    br.Close();
                    fs.Close();
                }
Yu | 园豆:12944 (专家六级) | 2014-12-02 21:13

@Yu: (int)wr.ContentLength;

这里出来是-1;不过这种思路解决我的问题了,谢谢YU

  using (WebResponse wr = req.GetResponse())
                {
                    Stream str = wr.GetResponseStream();
                    using (FileStream fileStream = new FileStream(string.Format("{0}ZhanZhao.pdf", DateTime.Now.ToString("yyyyMMddHHmmss")), FileMode.Create, FileAccess.Write))
                    {   
                        int size = 2048;
                        byte[] data = new byte[2048];
                        while (true)
                        {
                            size = str.Read(data, 0, data.Length);
                            if (size > 0)
                            {
                                fileStream.Write(data, 0, size);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
View Code

 

啸飛同学 | 园豆:133 (初学一级) | 2014-12-02 22:15
其他回答(3)
0

直接保存文件流就行了,既然网页已经返回的是PDF的文件流。

爱编程的大叔 | 园豆:30698 (高人七级) | 2014-12-02 14:27

大叔,代码是这样写的。

发起一个Http请求,返回的是一个Pdf数据流,我现在想要存储为一个Pdf文档,求指点啊~

byte[] bs = Encoding.UTF8.GetBytes(para);
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
req.Method = questMethod;
req.ContentType = "application/pdf";
req.ContentLength = bs.Length;

// 返回的流数据信息
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(bs, 0, bs.Length);
}

using (WebResponse wr = req.GetResponse())
{
StreamReader responseStream = new StreamReader(wr.GetResponseStream(), Encoding.UTF8);
strResponse = responseStream.ReadToEnd();
}

支持(0) 反对(0) 啸飛同学 | 园豆:133 (初学一级) | 2014-12-02 15:11

@啸飛同学: 我也遇到了,其实不论你是读本地pdf,还是从网页获取都是那样的。但是那样的如果你保存到文件时不会有问题的,如果你要返回到前台展示是有问题。你可以打开本地的pdf,以记事本打开,你会发现,同样的pdf,记事本有很多东西,明显要多的多,但是二进制流转换的字符串就会少很多。这个原因我找了很久也没找到,只能妥协了。

支持(0) 反对(0) inisiYou | 园豆:200 (初学一级) | 2016-11-30 17:52
0

直接创建文件xxx.pdf,写入返回来的stream,保存不就完了?

arg | 园豆:1047 (小虾三级) | 2014-12-02 16:43
0

网页转换为PDF现在非常简单了,不需要敲代码,可以直接去这个PDF在线转换平台https://www.pdf365.cn/上,将HTML格式的网页转换为PDF

foxit2world | 园豆:200 (初学一级) | 2019-03-04 09:33

你这个网址只能pdf转为其他的

支持(0) 反对(0) 麦兜疯168 | 园豆:200 (初学一级) | 2019-06-26 16:22
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册