简体   繁体   English

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

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

I have the following acceptance criteria for creating a pdf file from my asp.net page which contains nested RadGrid controls: 对于从包含嵌套RadGrid控件的asp.net页创建pdf文件,我具有以下接受标准:

  1. The current view of the page should be converted to PDF which means that the viewstate and session information of the current page request should be taken into account. 页面的当前视图应转换为PDF,这意味着应考虑当前页面请求的视图状态和会话信息。 This leaves me with only one option; 这给我只有一个选择。 make the PDF conversion at Page_Render() event handler of the current session when a new pdf postback is sent. 发送新的pdf回发时,在当前会话的Page_Render()事件处理程序中进行PDF转换。

  2. The asp.net page layout is changed using JQuery at the time of the $(document).ready(...) that means that not only the rendered HTML should be converted to PDF but also the javascripts have to run on it to have the desired layout changes in the final PDF file; $(document).ready(...)时,使用JQuery更改了asp.net页面布局,这意味着不仅应将呈现的HTML转换为PDF,还必须在其上运行javascript以具有最终的PDF文件中所需的布局更改; eg column alignments, etc. I hope it would be possible otherwise ... 例如列对齐,等等。我希望否则可能...

  3. The asp.net page only appears correctly in IE 6+ therefore the PDF tool which is used must use IE rendering engine. asp.net页仅在IE 6+中正确显示,因此使用的PDF工具必须使用IE渲染引擎。

Please could you advise which tool can help in such scenario? 请您告知在这种情况下哪种工具可以提供帮助?

I downloaded and tested EvoPdf tool but it doesn't support IE rendering engine apparently (only FireFox rendering) and couldn't make the javascripts enabling work correctly with it. 我下载并测试了EvoPdf工具,但它显然不支持IE渲染引擎(仅支持FireFox渲染),并且无法使javascripts与其一起正常工作。

I'm going to evaluate ABCPdf and Winnovetive but I'm not sure they would support what I want. 我将评估ABCPdf和Winnovetive,但不确定它们是否会支持我想要的内容。

If I could find no tool to help with the above, another possible solution might be just taking a screenshot of the page using client script (don't know whether it'd be possible), then sending it to the server and finally converting that image to pdf. 如果我找不到任何工具可以解决上述问题,则另一种可能的解决方案可能是使用客户端脚本截取页面的屏幕截图(不知道是否可能),然后将其发送到服务器,最后进行转换图片转pdf。

Many thanks, 非常感谢,

You can try WebToPDF.NET . 您可以尝试WebToPDF.NET

  1. Try to convert HTML page which you get after the asp.net page have been rendered 尝试转换呈现asp.net页面后获得的HTML页面
  2. WebToPDF.NET suports JavaScript(and JQuery), so it's not problem WebToPDF.NET支持JavaScript(和JQuery),所以这不是问题
  3. WebToPDF.NET passes all W3C tests (except BIDI) and supports HTML 4.01, JavaScript, XHTML 1.0, XHTML 1.1 and CSS 2.1 including page breaks, forms and links. WebToPDF.NET通过了所有W3C测试(BIDI除外),并支持HTML 4.01,JavaScript,XHTML 1.0,XHTML 1.1和CSS 2.1,包括分页符,表单和链接。

Winnovative HTML to PDF Converter does not use IE as rendering engine. Winnovative HTML to PDF Converter不使用IE作为呈现引擎。 It is compatible with WebKit rendering and does not depend on IE or any other third party tools. 它与WebKit呈现兼容,并且不依赖IE或任何其他第三方工具。

You can convert the current HTML page overriding the Render() method of the ASP.NET page and capture the HTML code being rendered by page. 您可以转换当前的HTML页面以覆盖ASP.NET页面的Render()方法,并捕获按页面呈现的HTML代码。 You can find complete example with C# source code in Convert the Current HTML Page to PDF Demo . 您可以在将当前HTML页面转换为PDF演示中找到带有C#源代码的完整示例。

Here is the relevant source code for this approach: 这是此方法的相关源代码:

// 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);
    }
}

Don't know exactly about your requirements but have a look at wkhtmltopdf 不完全了解您的要求,但请参阅wkhtmltopdf

How to use wkhtmltopdf.exe in ASP.net 如何在ASP.net中使用wkhtmltopdf.exe

winnovative did exactly what I needed :) it uses IE rendering engine unlike EvoPdf. winnovative完全满足了我的需要:)它使用与EvoPdf不同的IE渲染引擎。

I haven't had time testing other tools. 我没有时间测试其他工具。

Thanks 谢谢

EvoPdf is developed by the same team who develop ExpertPDF (http://www.html-to-pdf.net/). EvoPdf由开发ExpertPDF(http://www.html-to-pdf.net/)的同一团队开发。 ExpertPDF is the older product so although the APIs are almost identical, the EvoPDF API is slightly more refined. ExpertPDF是较旧的产品,因此尽管API几乎相同,但EvoPDF API却稍有改进。

The main difference between the products is that ExpertPDF uses the local IE rendering engine. 产品之间的主要区别是ExpertPDF使用本地IE渲染引擎。

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

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