gpt4 book ai didi

c# - wkhtmltopdf 输出流和下载 - 对话框

转载 作者:太空狗 更新时间:2023-10-29 21:46:13 25 4
gpt4 key购买 nike

是否可以从任何 html 文件获取由 wkhtmltopdf 创建的 pdf 流,并在 IE/Firefox/Chrome 等中弹出下载对话框?

目前我通过这段代码得到我的输出流:

public class Printer
{
public static MemoryStream GeneratePdf(StreamReader Html, MemoryStream pdf, Size pageSize)
{
Process p;
StreamWriter stdin;
ProcessStartInfo psi = new ProcessStartInfo();

psi.FileName = @"C:\PROGRA~1\WKHTML~1\wkhtmltopdf.exe";

// run the conversion utility
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;

// note that we tell wkhtmltopdf to be quiet and not run scripts
psi.Arguments = "-q -n --disable-smart-shrinking " + (pageSize.IsEmpty ? "" : "--page-width " + pageSize.Width + "mm --page-height " + pageSize.Height + "mm") + " - -";

p = Process.Start(psi);

try
{
stdin = p.StandardInput;
stdin.AutoFlush = true;
stdin.Write(Html.ReadToEnd());
stdin.Dispose();

CopyStream(p.StandardOutput.BaseStream, pdf);
p.StandardOutput.Close();
pdf.Position = 0;

p.WaitForExit(10000);

return pdf;
}
catch
{
return null;
}
finally
{
p.Dispose();
}
}

public static void CopyStream(Stream input, Stream output)
{
byte[] buffer = new byte[32768];
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, read);
}
}
}

然后我要显示对话框:

MemoryStream PDF = Printer.GeneratePdf(Rd, PDFStream, Size);

byte[] byteArray1 = PDF.ToArray();
PDF.Flush();
PDF.Close();
Response.BufferOutput = true;

Response.Clear();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "attachment; filename=Test.pdf");
Response.ContentType = "application/octet-stream";
Response.BinaryWrite(byteArray1);
Response.End();

对于从 PDF 文件创建的 MemoryStreams,这工作正常,但在这里我只得到一个空白页面。 bytearray 有 1270 字节。

最佳答案

这还是个问题吗?

在安装 wkhtmltopdf 0.11.0 rc2 后,我刚刚创建了一个新的 ASP.net 网站来在我的计算机上进行测试,它创建 PDF 时运行良好。我的版本只是略有不同;

在我的 CSHTML 中我有:

MemoryStream PDFStream = new MemoryStream();
MemoryStream PDF = Derp.GeneratePdf(PDFStream);
byte[] byteArray1 = PDF.ToArray();
PDF.Flush();
PDF.Close();
Response.BufferOutput = true;
Response.Clear();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "attachment; filename=Test.pdf");
Response.ContentType = "application/octet-stream";
Response.BinaryWrite(byteArray1);
Response.End();

我的 Derp 类

public class Derp
{
public static MemoryStream GeneratePdf(MemoryStream pdf)
{
using (StreamReader Html = new StreamReader(@"Z:\HTMLPage.htm"))
{
Process p;
StreamWriter stdin;
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = @"C:\wkhtmltopdf\wkhtmltopdf.exe";
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.Arguments = "-q -n --disable-smart-shrinking " + " - -";
p = Process.Start(psi);
try
{
stdin = p.StandardInput;
stdin.AutoFlush = true;
stdin.Write(Html.ReadToEnd());
stdin.Dispose();
CopyStream(p.StandardOutput.BaseStream, pdf);
p.StandardOutput.Close();
pdf.Position = 0;
p.WaitForExit(10000);
return pdf;
}
catch
{
return null;
}
finally
{
p.Dispose();
}
}
}

public static void CopyStream(Stream input, Stream output)
{
byte[] buffer = new byte[32768];
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, read);
}
}
}

关于c# - wkhtmltopdf 输出流和下载 - 对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10292689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com