我已经在asp.net上创建了一个Web表单。这个想法是我将一个用WebForm.aspx输入填充的html文件转换为pdf文件,并在按钮单击事件下将其下载。我曾尝试使用IronPDF,但每次单击按钮时,它只会一次又一次加载Web表单。这是代码:
protected void btnConvertToPdf_Click(object sender, EventArgs e)
{
var Render = new IronPdf.HtmlToPdf();
var PDF = Render.RenderHTMLFileAsPdf("C:/Users/silvio/source/repos/WebFormSilvio/WebFormSilvio/Views/HtmlPage1.html");
var Outpupath = "C:/Users/silvio/Downloads/Form1.pdf";
PDF.SaveAs(Outpupath);
}
protected void btnUpload_Click(object sender, EventArgs e)
{
if (FileUploadCtrl.HasFile)
{
try
{
string filename = Path.GetFileName(FileUploadCtrl.FileName);
FileUploadCtrl.SaveAs(Server.MapPath("~/") + filename);
StatusLabel.Text = "Upload Status: Dosja u ngarkua!";
}
catch(Exception ex)
{
StatusLabel.Text = "Upload status: Dosja nuk u ngarkua. Ndodhi nje error:" + ex.Message;
}
}
}
}
答案 0 :(得分:1)
我认为您应该为客户端流式传输文件pdf:
var Render = new IronPdf.HtmlToPdf();
var PDF = Render.RenderHTMLFileAsPdf("C:/Users/silvio/source/repos/WebFormSilvio/WebFormSilvio/Views/HtmlPage1.html");
var Outpupath = "C:/Users/silvio/Downloads/Form1.pdf";
PDF.SaveAs(Outpupath);
//attach the file to the reponse and return it
var fileInfo = new System.IO.FileInfo(outputPath);
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", String.Format("attachment;filename=\"{0}\"", outputPath));
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.WriteFile(outputPath);
Response.End();
如果pdf很大,则应使用Response.TransmitFile
而不是Response.WriteFile
。