C# 无法在服务器上将HTML文件转换为PDF
我尝试使用pechkin.dll将HTML文件转换为PDF。在本地主机上,它可以工作。但当我将项目上传到服务器时,我无法转换文件。我得到一个错误:“内部服务器错误500”。你能告诉我怎么解决这个问题吗?请帮忙 这是一个ajax脚本(工作正常) 这是我在代码隐藏中转换文件的代码C# 无法在服务器上将HTML文件转换为PDF,c#,asp.net,asp.net-ajax,wkhtmltopdf,pechkin,C#,Asp.net,Asp.net Ajax,Wkhtmltopdf,Pechkin,我尝试使用pechkin.dll将HTML文件转换为PDF。在本地主机上,它可以工作。但当我将项目上传到服务器时,我无法转换文件。我得到一个错误:“内部服务器错误500”。你能告诉我怎么解决这个问题吗?请帮忙 这是一个ajax脚本(工作正常) 这是我在代码隐藏中转换文件的代码 [System.Web.Services.WebMethod(EnableSession = true)] public static string test(string test) { convertFunct
[System.Web.Services.WebMethod(EnableSession = true)]
public static string test(string test)
{
convertFunction(test);
return test;
}
private static void convertFunction(string test)
{
int id = Convert.ToInt32(HttpContext.Current.Session["insertID"].ToString());
string fileName1 = @"D:\web\Mcer\HtmlPage1.html";
if (!Directory.Exists(Path.GetDirectoryName(fileName1)))
{
Directory.CreateDirectory(Path.GetDirectoryName(fileName1));
FileStream file = File.Create(fileName1);
StreamWriter wt = new StreamWriter(file, Encoding.UTF8);
wt.WriteLine(test);
file.Close();
}
else
{
File.Delete(fileName1);
FileStream file = File.Create(fileName1);
StreamWriter wt = new StreamWriter(file, Encoding.UTF8);
wt.WriteLine(test);
file.Close();
}
//convert html to pdf
var pechkin = Factory.Create(new GlobalConfig());
var pdf = pechkin.Convert(new ObjectConfig()
.SetLoadImages(true).SetZoomFactor(1.5)
.SetPrintBackground(true)
.SetScreenMediaType(true)
.SetCreateExternalLinks(true)
.SetPageUri("http://192.168.24.36/medcer/HtmlPage1.html"));
//return pdf file
SqlConnection EMR_Conn = new SqlConnection(@"Data Source=192.168.24.36;Initial Catalog=EMR;Persist Security Info=True;User ID=sa;Password=bpk$1234");
EMR_Conn.Open();
SqlCommand cmd = new SqlCommand("select hncode from MED_main where rowID = '" + id + "'", EMR_Conn);
SqlDataReader dr;
DataTable dt;
String hn = "";
string printerName = HttpContext.Current.Session["pathPrint"].ToString();
dr = cmd.ExecuteReader();
dt = new DataTable();
dt.Load(dr);
if (dt.Rows.Count != 0)
{
hn = dt.Rows[0]["hncode"].ToString().Trim();
}
dr.Close();
EMR_Conn.Close();
if (hn != "")
{
string fileName = @"D:\web\Mcer\PDFs\" + id + ")" + hn + ".pdf";
if (!Directory.Exists(Path.GetDirectoryName(fileName)))
{
Directory.CreateDirectory(Path.GetDirectoryName(fileName));
FileStream file = File.Create(fileName);
file.Write(pdf, 0, pdf.Length);
file.Close();
//print function
Process printJob = new Process();
printJob.StartInfo.FileName = fileName;
printJob.StartInfo.Arguments = printerName;
printJob.StartInfo.Verb = "printto";
printJob.StartInfo.CreateNoWindow = true;
printJob.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
PrinterSettings setting = new PrinterSettings();
setting.DefaultPageSettings.Landscape = true;
printJob.Start();
printJob.Close();
}
else
{
File.Delete(fileName);
FileStream file = File.Create(fileName);
file.Write(pdf, 0, pdf.Length);
file.Close();
//print function
Process printJob = new Process();
printJob.StartInfo.FileName = fileName;
printJob.StartInfo.Arguments = printerName;
printJob.StartInfo.Verb = "printto";
printJob.StartInfo.CreateNoWindow = true;
printJob.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
PrinterSettings setting = new PrinterSettings();
setting.DefaultPageSettings.Landscape = true;
printJob.Start();
printJob.Close();
}
}
}
请帮帮我。非常感谢。我认为您的硬编码文件名1值可能是问题所在,您确定您的服务器上存在“D:\web\Mcer\HtmlPage1.html”吗?我已经解决了这个问题,因为服务器端没有adobe reader。谢谢你的回答请加上这个作为接受的答案,所以这个问题被标记为已回答。
[System.Web.Services.WebMethod(EnableSession = true)]
public static string test(string test)
{
convertFunction(test);
return test;
}
private static void convertFunction(string test)
{
int id = Convert.ToInt32(HttpContext.Current.Session["insertID"].ToString());
string fileName1 = @"D:\web\Mcer\HtmlPage1.html";
if (!Directory.Exists(Path.GetDirectoryName(fileName1)))
{
Directory.CreateDirectory(Path.GetDirectoryName(fileName1));
FileStream file = File.Create(fileName1);
StreamWriter wt = new StreamWriter(file, Encoding.UTF8);
wt.WriteLine(test);
file.Close();
}
else
{
File.Delete(fileName1);
FileStream file = File.Create(fileName1);
StreamWriter wt = new StreamWriter(file, Encoding.UTF8);
wt.WriteLine(test);
file.Close();
}
//convert html to pdf
var pechkin = Factory.Create(new GlobalConfig());
var pdf = pechkin.Convert(new ObjectConfig()
.SetLoadImages(true).SetZoomFactor(1.5)
.SetPrintBackground(true)
.SetScreenMediaType(true)
.SetCreateExternalLinks(true)
.SetPageUri("http://192.168.24.36/medcer/HtmlPage1.html"));
//return pdf file
SqlConnection EMR_Conn = new SqlConnection(@"Data Source=192.168.24.36;Initial Catalog=EMR;Persist Security Info=True;User ID=sa;Password=bpk$1234");
EMR_Conn.Open();
SqlCommand cmd = new SqlCommand("select hncode from MED_main where rowID = '" + id + "'", EMR_Conn);
SqlDataReader dr;
DataTable dt;
String hn = "";
string printerName = HttpContext.Current.Session["pathPrint"].ToString();
dr = cmd.ExecuteReader();
dt = new DataTable();
dt.Load(dr);
if (dt.Rows.Count != 0)
{
hn = dt.Rows[0]["hncode"].ToString().Trim();
}
dr.Close();
EMR_Conn.Close();
if (hn != "")
{
string fileName = @"D:\web\Mcer\PDFs\" + id + ")" + hn + ".pdf";
if (!Directory.Exists(Path.GetDirectoryName(fileName)))
{
Directory.CreateDirectory(Path.GetDirectoryName(fileName));
FileStream file = File.Create(fileName);
file.Write(pdf, 0, pdf.Length);
file.Close();
//print function
Process printJob = new Process();
printJob.StartInfo.FileName = fileName;
printJob.StartInfo.Arguments = printerName;
printJob.StartInfo.Verb = "printto";
printJob.StartInfo.CreateNoWindow = true;
printJob.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
PrinterSettings setting = new PrinterSettings();
setting.DefaultPageSettings.Landscape = true;
printJob.Start();
printJob.Close();
}
else
{
File.Delete(fileName);
FileStream file = File.Create(fileName);
file.Write(pdf, 0, pdf.Length);
file.Close();
//print function
Process printJob = new Process();
printJob.StartInfo.FileName = fileName;
printJob.StartInfo.Arguments = printerName;
printJob.StartInfo.Verb = "printto";
printJob.StartInfo.CreateNoWindow = true;
printJob.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
PrinterSettings setting = new PrinterSettings();
setting.DefaultPageSettings.Landscape = true;
printJob.Start();
printJob.Close();
}
}
}