【问题标题】:ASP.NET converting HTML to PDFASP.NET 将 HTML 转换为 PDF
【发布时间】:2017-07-17 04:16:48
【问题描述】:

我尝试了多个插件和 c# 类来尝试将我的 asp.net 项目中的 HTML 和 CSS 转换为 pdf,即使使用的代码看起来不错,并且按钮单击适用于其他功能,但我似乎无法让任何 html 到 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");


}

【问题讨论】:

  • 不起作用是什么意思?错误?请更具体。
  • 嗨@SamiKuhmonen,我运行代码时没有任何反应

标签: c# html css asp.net pdf


【解决方案1】:

该方法的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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-04
    • 2011-07-01
    • 1970-01-01
    • 2011-09-02
    • 2010-12-31
    • 2014-03-02
    • 1970-01-01
    相关资源
    最近更新 更多