繁体   English   中英

ASP.NET网页转PDF

[英]ASP.NET web page to PDF

我正在寻找使用iTextSharp将当前查看的asp.net网页导出到PDF文档的最简单方法-它可以是它的屏幕截图,也可以传入URL来生成文档。 示例代码将不胜感激。 提前谢谢了!

在过去的项目中,我们使用Supergoo ABCPDF来完成您需要的操作,并且效果很好。 基本上,您为它提供一个URL,然后将HTML页面处理为PDF。

不利的一面是,它是一款许可软件,但与此相关,但同时导出大量大型PDF时,我们遇到了一些性能问题。

希望这可以帮助 !

WInnovative网站上有一个将当前HTML页面转换为PDF的示例。 将当前查看的asp.net网页转换为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生成PDF文件。 它以URL作为输入。 这就是我用于SO应用程序so2pdf的内容

这不是那么容易(或者我认为是这样),我遇到了同样的问题,我不得不编写代码以生成pdf中的确切页面。 它取决于页面和使用的样式等。因此,我创建了每个元素的图形。

对于某些项目,我使用Winnovative HTML到PDF转换器,但它不是免费的。

试试看PDFSharp(在运行时生成PDF文件),它是开源库: http ://pdfsharp.com/PDFsharp/index.php?option=com_content&task=view&id=27&Itemid=1

替代解决方案: http : //www.bratched.com/en/component/content/article/76-how-to-convert-xhtml-to-pdf-in-c.html

暂无
暂无

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

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