当前位置: 首页 > 编程语言 > C#

wkhtmltopdf输出流和下载-diaglog分享

时间:2023-04-10 15:22:21 C#

wkhtmltopdf输出流和下载-diaglog是否可以从任何html文件中获取wkhtmltopdf创建的pdf流并在IE/Firefox/Chrome等中弹出下载对话框。框架?目前我通过这段代码得到我的输出流:StreamWriter标准输入;ProcessStartInfopsi=newProcessStartInfo();psi.FileName=@"C:PROGRA~1WKHTML~1wkhtmltopdf.exe";//运行转换工具psi.UseShellExecute=false;psi.CreateNoWindow=true;psi.RedirectStandardInput=true;psi.RedirectStandardOutput=true;psi.RedirectStandardError=true;//请注意,我们告诉wkhtmltopdf安静并且不运行脚本宽度+"mm--page-height"+pageSize.Height+"mm")+"--";p=Process.Start(psi);尝试{stdin=p.StandardInput;stdin.AutoFlush=true;stdin.Write(Html.ReadToEnd());标准输入显示罗斯();CopyStream(p.StandardOutput.BaseStream,pdf);p.StandardOutput.Close();pdf.位置=0;p.WaitForExit(10000);返回pdf;}赶上{返回空;}最后{p.处置();}}publicstaticvoidCopyStream(Streaminput,Streamoutput){byte[]buffer=newbyte[32768];诠释阅读;while((read=input.Read(buffer,0,buffer.Length))>0){output.Write(buffer,0,read);然后我想显示对话框:MemoryStreamPDF=Printer.GeneratePdf(Rd,PDFStream,Size);byte[]byteArray1=PDF.ToArray();PDF.Flush();PDF.关闭();Response.BufferOutput=true;Response.Clear();Response.ClearHeaders();Response.AddHeader("Content-Disposition","attachment;filename=Test.pdf");Response.ContentType="application/octet-stream";Response.BinaryWrite(byteArray1);响应.End();使用从PDF文件创建的MemoryStream工作正常,但在这里我只得到一个包含1270个单词Festival的空页字节数组。这仍然是一个问题吗?我刚刚创建了一个新的ASP.net网站,在安装wkhtmltopdf0.11.0rc2后在我的计算机上对其进行了测试,它可以很好地创建PDF。我的版本只是略有不同;在我的CSHTML中,我有:MemoryStreamPDFStream=newMemoryStream();MemoryStreamPDF=Derp.GeneratePdf(PDFStream);byte[]byteArray1=PDF.ToArray();PDF.Flush();PDF.关闭();Response.BufferOutput=true;Response.Clear();Response.ClearHeaders();Response.AddHeader("Content-Disposition","attachment;filename=Test.pdf");Response.ContentType="application/octet-stream";Response.BinaryWrite(byteArray1);响应.End();以上我的Derp课程是C#学习教程:wkhtmltopdf输出流及下载——diaglog分享的所有内容,如果对大家有用还有我需要了解更多C#学习教程,希望大家多多关注——publicclassDerp{publicstaticMemoryStreamGeneratePdf(MemoryStreampdf){使用(StreamReaderHtml=newStreamReader(@"Z:HTMLPage.htm")){Processp;StreamWriter标准输入;ProcessStartInfopsi=newProcessStartInfo();psi.FileName=@"C:wkhtmltopdfwkhtmltopdf.exe";psi.UseShellExecute=false;psi.CreateNoWindow=true;psi.RedirectStandardInput=true;psi.RedirectStandardOutput=true;psi。重定向标准错误=真;psi.Arguments="-q-n--disable-smart-shrinking"+"--";p=Process.Start(psi);尝试{stdin=p.StandardInput;stdin.AutoFlush=true;标准输入.Write(Html.ReadToEnd());标准输入.Dispose();CopyStream(p.StandardOutput.BaseStream,pdf);p.StandardOutput.Close();pdf.位置=0;p.WaitForExit(10000);返回pdf;}赶上{返回空;}最后{p.Dispose();}}}publicstaticvoidCopyStream(Streaminput,Streamoutput){byte[]buffer=newbyte[32768];诠释阅读;while((read=input.Read(buffer,0,buffer.Length))>0){output.Write(buffer,0,read);}}}本文收集自网络,不代表立场。如涉及侵权请点击维权联系管理员删除如需转载请注明出处: