Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在内存中制作PDF并将其发送到浏览器_Javascript_C#_Asp.net_Pdf_Memorystream - Fatal编程技术网

Javascript 如何在内存中制作PDF并将其发送到浏览器

Javascript 如何在内存中制作PDF并将其发送到浏览器,javascript,c#,asp.net,pdf,memorystream,Javascript,C#,Asp.net,Pdf,Memorystream,我想从内存中的HTML字符串生成PDF,并将其发送到浏览器。 我不想在服务器上保存文件,因此我使用以下代码。我也在使用ITextSharp string thistopdf = "<div>ESto es una prueba</div>"; StringReader sr = new StringReader(thistopdf); Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f); HTM

我想从内存中的HTML字符串生成PDF,并将其发送到浏览器。 我不想在服务器上保存文件,因此我使用以下代码。我也在使用ITextSharp

string thistopdf = "<div>ESto es una prueba</div>";

StringReader sr = new StringReader(thistopdf);

Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
using (MemoryStream memoryStream = new MemoryStream())
{            
    PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
    byte[] bytes = memoryStream.ToArray();
    HttpContext.Current.Response.Buffer = false;
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.ClearContent();
    HttpContext.Current.Response.ClearHeaders();
    HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=test.pdf");
    HttpContext.Current.Response.ContentType = "Application/pdf";
    HttpContext.Current.Response.BinaryWrite(bytes);
    HttpContext.Current.Response.Flush();
    HttpContext.Current.Response.End();            
}
我收到的数据是这样的

%PDF-1.4%����2 0 obj<</Length 70/Filter/FlateDecode>>streamx�+�r�24P�06R%08I�2P�5�%001��%0C%15%0C�Bi\%1A��%����y�%05E��I��!Y@�%06 u�!\�\%002�%10�endstreamendobj4 0 obj<</Type/Page/MediaBox[0 0 595 842]/Resources<</Font<</F1 1 0 R>>>>/Contents 2 0 R/Parent 3 0 R>>endobj1 0 obj<</Type/Font/Subtype/Type1/BaseFont/Helvetica/Encoding/WinAnsiEncoding>>endobj3 0 obj<</Type/Pages/Count 1/Kids[4 0 R]>>endobj5 0 obj<</Type/Catalog/Pages 3 0 R>>endobj6 0 obj<</Producer(iTextSharp� 5.4.4 �2000-2013 1T3XT BVBA \(AGPL-version\))/CreationDate(D:20151005114723-03'00')/ModDate(D:20151005114723-03'00')>>endobjxref0 70000000000 65535 f 0000000263 00000 n 0000000015 00000 n 0000000351 00000 n 0000000151 00000 n 0000000402 00000 n 0000000447 00000 n trailer<</Size 7/Root 5 0 R/Info 6 0 R/ID [<9f72015ead133e6b8d3562d038bf19df><c7c6b8e193f64b5157b8b62e94bc802e>]>>%iText-5.4.4startxref605%%EOF
%PDF-1.4%����2 0 objstreamx�+�R�24便士�06R%08I�2P�5.�%001��%0C%15%0C�Bi\%1A��%����Y�%05E��我��!Y@�%06 u�!\�\%002�%10�endstreamendobj4 0 objendobj1 0 objendobj3 0 objendobj5 0 objendobj6 0 objendobjxref0 7000000000 65535 f 0000000 26300000 n 00000000 15 00000 n 0000000 351000000 n 0000000 151 00000 n 0000000 40200000 n 0000000 44700000 n拖车%iText-5.4.4startxref605%%EOF
我做错了什么?我找不到解决这个问题的办法。
它是在服务器上还是在JavaScript上?

我认为这个问题已经得到了回答,您需要FileContentResult。我已经回答了类似的问题。对你们来说唯一的区别就是改变了MediaTypeName。@搜信你们是在假设MVC。这很可能是Web表单,
FileContentResult
与此无关。
数据:应用程序/pdf
样式的URL并非所有浏览器/pdf查看器插件组合都普遍支持。我将字节转换为base64字符串,然后改为window.open(“数据:应用程序/pdf;base64,+data”);在javascript中,它成功了。我会用我后来找到的答案编辑这个问题。我想这个问题已经回答了。你需要FileContentResult。我已经回答了类似的问题。对你们来说唯一的区别就是改变了MediaTypeName。@搜信你们是在假设MVC。这很可能是Web表单,
FileContentResult
与此无关。
数据:应用程序/pdf
样式的URL并非所有浏览器/pdf查看器插件组合都普遍支持。我将字节转换为base64字符串,然后改为window.open(“数据:应用程序/pdf;base64,+data”);在javascript中,它成功了。我将用稍后找到的答案编辑问题。
%PDF-1.4%����2 0 obj<</Length 70/Filter/FlateDecode>>streamx�+�r�24P�06R%08I�2P�5�%001��%0C%15%0C�Bi\%1A��%����y�%05E��I��!Y@�%06 u�!\�\%002�%10�endstreamendobj4 0 obj<</Type/Page/MediaBox[0 0 595 842]/Resources<</Font<</F1 1 0 R>>>>/Contents 2 0 R/Parent 3 0 R>>endobj1 0 obj<</Type/Font/Subtype/Type1/BaseFont/Helvetica/Encoding/WinAnsiEncoding>>endobj3 0 obj<</Type/Pages/Count 1/Kids[4 0 R]>>endobj5 0 obj<</Type/Catalog/Pages 3 0 R>>endobj6 0 obj<</Producer(iTextSharp� 5.4.4 �2000-2013 1T3XT BVBA \(AGPL-version\))/CreationDate(D:20151005114723-03'00')/ModDate(D:20151005114723-03'00')>>endobjxref0 70000000000 65535 f 0000000263 00000 n 0000000015 00000 n 0000000351 00000 n 0000000151 00000 n 0000000402 00000 n 0000000447 00000 n trailer<</Size 7/Root 5 0 R/Info 6 0 R/ID [<9f72015ead133e6b8d3562d038bf19df><c7c6b8e193f64b5157b8b62e94bc802e>]>>%iText-5.4.4startxref605%%EOF