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

发布于 2024-11-26 04:28:45 字数 686 浏览 2 评论 0原文

我有以下接受标准从包含嵌套 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 渲染),并且无法使 javascript 能够正确使用它。

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

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

非常感谢,

I have the following acceptance criteria for creating a pdf file from my asp.net page which contains nested RadGrid controls:

  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. 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.

  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; e.g. 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.

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.

I'm going to evaluate ABCPdf and Winnovetive but I'm not sure they would support what I want.

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.

Many thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

別甾虛僞 2024-12-03 04:28:45

您可以尝试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,包括分页符、表单和链接。

You can try WebToPDF.NET.

  1. Try to convert HTML page which you get after the asp.net page have been rendered
  2. WebToPDF.NET suports JavaScript(and JQuery), so it's not problem
  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.
欲拥i 2024-12-03 04:28:45

不太清楚您的要求,但请查看 wkhtmltopdf

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

Don't know exactly about your requirements but have a look at wkhtmltopdf

How to use wkhtmltopdf.exe in ASP.net

ㄖ落Θ余辉 2024-12-03 04:28:45

winnovative 正是我所需要的:)它使用 IE 渲染引擎,与 EvoPdf 不同。

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

谢谢

winnovative did exactly what I needed :) it uses IE rendering engine unlike EvoPdf.

I haven't had time testing other tools.

Thanks

尛丟丟 2024-12-03 04:28:45

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

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

EvoPdf is developed by the same team who develop 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.

The main difference between the products is that ExpertPDF uses the local IE rendering engine.

旧时模样 2024-12-03 04:28:45

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

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

以下是该方法的相关源代码:

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

Winnovative HTML to PDF Converter does not use IE as rendering engine. It is compatible with WebKit rendering and does not depend on IE or any other third party tools.

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. You can find complete example with C# source code in Convert the Current HTML Page to PDF Demo.

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文