繁体   English   中英

如何将我的ASP.NET页面转换为PDF?

[英]How to Convert my ASP.NET Page to PDF?

对于从包含嵌套RadGrid控件的asp.net页创建pdf文件,我具有以下接受标准:

  1. 页面的当前视图应转换为PDF,这意味着应考虑当前页面请求的视图状态和会话信息。 这给我只有一个选择。 发送新的pdf回发时,在当前会话的Page_Render()事件处理程序中进行PDF转换。

  2. $(document).ready(...)时,使用JQuery更改了asp.net页面布局,这意味着不仅应将呈现的HTML转换为PDF,还必须在其上运行javascript以具有最终的PDF文件中所需的布局更改; 例如列对齐,等等。我希望否则可能...

  3. asp.net页仅在IE 6+中正确显示,因此使用的PDF工具必须使用IE渲染引擎。

请您告知在这种情况下哪种工具可以提供帮助?

我下载并测试了EvoPdf工具,但它显然不支持IE渲染引擎(仅支持FireFox渲染),并且无法使javascripts与其一起正常工作。

我将评估ABCPdf和Winnovetive,但不确定它们是否会支持我想要的内容。

如果我找不到任何工具可以解决上述问题,则另一种可能的解决方案可能是使用客户端脚本截取页面的屏幕截图(不知道是否可能),然后将其发送到服务器,最后进行转换图片转pdf。

非常感谢,

您可以尝试WebToPDF.NET

  1. 尝试转换呈现asp.net页面后获得的HTML页面
  2. WebToPDF.NET支持JavaScript(和JQuery),所以这不是问题
  3. WebToPDF.NET通过了所有W3C测试(BIDI除外),并支持HTML 4.01,JavaScript,XHTML 1.0,XHTML 1.1和CSS 2.1,包括分页符,表单和链接。

Winnovative HTML to PDF Converter不使用IE作为呈现引擎。 它与WebKit呈现兼容,并且不依赖IE或任何其他第三方工具。

您可以转换当前的HTML页面以覆盖ASP.NET页面的Render()方法,并捕获按页面呈现的HTML代码。 您可以在将当前HTML页面转换为PDF演示中找到带有C#源代码的完整示例。

这是此方法的相关源代码:

// Controls if the current HTML page will be rendered to PDF or as a normal page
bool convertToPdf = false;

protected void convertToPdfButton_Click(object sender, EventArgs e)
{
    // The current ASP.NET page will be rendered to PDF when its Render method will be called by framework
    convertToPdf = true;
}

protected override void Render(HtmlTextWriter writer)
{
    if (convertToPdf)
    {
        // Get the current page HTML string by rendering into a TextWriter object
        TextWriter outTextWriter = new StringWriter();
        HtmlTextWriter outHtmlTextWriter = new HtmlTextWriter(outTextWriter);
        base.Render(outHtmlTextWriter);

        // Obtain the current page HTML string
        string currentPageHtmlString = outTextWriter.ToString();

        // Create a HTML to PDF converter object with default settings
        HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();

        // Set license key received after purchase to use the converter in licensed mode
        // Leave it not set to use the converter in demo mode
        htmlToPdfConverter.LicenseKey = "fvDh8eDx4fHg4P/h8eLg/+Dj/+jo6Og=";

        // Use the current page URL as base URL
        string baseUrl = HttpContext.Current.Request.Url.AbsoluteUri;

        // Convert the current page HTML string a PDF document in a memory buffer
        byte[] outPdfBuffer = htmlToPdfConverter.ConvertHtml(currentPageHtmlString, baseUrl);

        // Send the PDF as response to browser

        // Set response content type
        Response.AddHeader("Content-Type", "application/pdf");

        // Instruct the browser to open the PDF file as an attachment or inline
        Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Convert_Current_Page.pdf; size={0}", outPdfBuffer.Length.ToString()));

        // Write the PDF document buffer to HTTP response
        Response.BinaryWrite(outPdfBuffer);

        // End the HTTP response and stop the current page processing
        Response.End();
    }
    else
    {
        base.Render(writer);
    }
}

不完全了解您的要求,但请参阅wkhtmltopdf

如何在ASP.net中使用wkhtmltopdf.exe

winnovative完全满足了我的需要:)它使用与EvoPdf不同的IE渲染引擎。

我没有时间测试其他工具。

谢谢

EvoPdf由开发ExpertPDF(http://www.html-to-pdf.net/)的同一团队开发。 ExpertPDF是较旧的产品,因此尽管API几乎相同,但EvoPDF API却稍有改进。

产品之间的主要区别是ExpertPDF使用本地IE渲染引擎。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM