ASP.NET将HTML转换为PDF

时间:2017-02-26 15:56:29

标签: c# html css asp.net pdf

我已经尝试了多个插件和c#类来尝试将我的asp.net项目上的HTML和CSS转换为pdf,即使使用的代码看起来很好,按钮单击适用于其他功能,我似乎无法获取任何html to pdf函数。有没有其他人遇到这个,或者知道我有什么遗漏可以解决它?

这是我在C#中为hiqpdf尝试的最新代码:

protected void Print_Button_Click(object sender, EventArgs e)
{


    HtmlToPdf htmlToPdfConverter = new HtmlToPdf();

    // set PDF page size, orientation and margins
    htmlToPdfConverter.Document.PageSize = PdfPageSize.A4;
    htmlToPdfConverter.Document.PageOrientation = PdfPageOrientation.Portrait;
    htmlToPdfConverter.Document.Margins = new PdfMargins(0);

    // convert HTML to PDF 
    htmlToPdfConverter.ConvertUrlToFile("http://localhost:51091/Printout","mcn.pdf");


}

1 个答案:

答案 0 :(得分:0)

该方法的HiQpdf文档中未直接说明,但方法ConvertUrlToFile()将生成的pdf文件本地存储在光盘上。在某些示例页面上(将URL和HTML代码转换为PDF),可以找到以下注释:

        // ConvertUrlToFile() is called to convert the html document and save the resulted PDF into a file on disk
        // Alternatively, ConvertUrlToMemory() can be called to save the resulted PDF in a buffer in memory
        htmlToPdfConverter.ConvertUrlToFile(url, pdfFile);

由于您的示例显示了一个按钮单击事件处理程序,因此该文件可能已生成但未用于在http响应中返回。您必须将数据写入响应。方法ConvertToStream()ConvertToMemory应该派上用场。不要忘记在此之前使用Response.Clear()Response.ClearContent()Response.ClearHeader(),之后再使用Flush() and Close()

相关问题