网页直接返回的是一个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);
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=filename.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();
}
这样设置了还是不得行~
取回来的数据样式是这样的~
%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��
@啸飛同学: 那你咋么保存的?
@Yu:前面的方法会等到一个strResponse,然后用ITextSharp创建了个文件保存的~
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();
@啸飛同学:
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: 还是一样的~
%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);
@啸飛同学:
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: (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; } } } }
直接保存文件流就行了,既然网页已经返回的是PDF的文件流。
大叔,代码是这样写的。
发起一个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();
}
@啸飛同学: 我也遇到了,其实不论你是读本地pdf,还是从网页获取都是那样的。但是那样的如果你保存到文件时不会有问题的,如果你要返回到前台展示是有问题。你可以打开本地的pdf,以记事本打开,你会发现,同样的pdf,记事本有很多东西,明显要多的多,但是二进制流转换的字符串就会少很多。这个原因我找了很久也没找到,只能妥协了。
直接创建文件xxx.pdf,写入返回来的stream,保存不就完了?
网页转换为PDF现在非常简单了,不需要敲代码,可以直接去这个PDF在线转换平台https://www.pdf365.cn/上,将HTML格式的网页转换为PDF
你这个网址只能pdf转为其他的