您可以尝试使用此工具,这对HTML到pdf的对话非常有用http://wkhtmltopdf.org/
这是将HTML字符串转换为PDF字节的代码。
public byte[] Convert(string source, string commandLocation)
{
Process p;
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = commandLocation;
psi.WorkingDirectory = @"D:\" ;// Path.GetDirectoryName(psi.FileName);
// 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
string args = "-q -n ";
args += "--disable-smart-shrinking ";
args += "";
args += "--outline-depth 0 ";
args += "--page-size A4 ";
args += " - -";
psi.Arguments = args;
p = Process.Start(psi);
try
{
using (StreamWriter stdin = p.StandardInput)
{
stdin.AutoFlush = true;
stdin.Write(source);
}
//read output
byte[] buffer = new byte[32768];
byte[] file;
using (var ms = new MemoryStream())
{
while (true)
{
int read = p.StandardOutput.BaseStream.Read(buffer, 0, buffer.Length);
if (read <= 0)
break;
ms.Write(buffer, 0, read);
}
file = ms.ToArray();
}
p.StandardOutput.Close();
// wait or exit
p.WaitForExit(60000);
// read the exit code, close process
int returnCode = p.ExitCode;
p.Close();
if (returnCode == 0)
return file;
else
LogManager.Log("Could not create PDF, returnCode:" + returnCode);
}
catch (Exception ex)
{
LogManager.Log("Could not create PDF", ex);
}
finally
{
p.Close();
p.Dispose();
}
return null;
}
这里的“源代码”是HTML代码,而commandLocation是wkhtmltopdf.exe的完整文件路径,您可以从上面的链接下载。该函数返回byte [],这是pdf文件,您可以将其保存在磁盘上,也可以通过编写Response.Write(byte)返回到浏览器;并更改MIMIE类型。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句