如何将当前ASPX页面导出为PDF

时间:2010-02-24 22:13:21

标签: asp.net html pdf-generation export-to-pdf

我有一个网页,它使用gridviews和html元素的组合来创建报告。我想给用户一个单击选项,它将导出为pdf。我不想在服务器上创建pdf,因为这需要我编写另一个进程来清理文件。

最理想的情况是,我希望当前页面打开一个新页面,该页面呈现pdf并提示用户保存/打开它。

我看过iTextSharp并且如果我不必指定每个元素,我有兴趣使用它。如果有办法指定一个面板及其所有内容或替代方案,我也会对此持开放态度。

谢谢!

2 个答案:

答案 0 :(得分:2)

如果您可以说服用户安装PDF CutePDfFoxit PDF Creator

等PDF打印机,那将会很容易

答案 1 :(得分:1)

我知道这是一个老问题,但我的公司做的事与你所需要的非常相似。我们使用ExpertPDF将html转换为pdf并执行以下操作以获取html。重要的是你将 EnableEventValidation =“false”转发回服务器是一种安全隐患。

ASPX

<%@ Page Language="C#" **EnableEventValidation="false"** Inherits="MyProject.Default"
        Title="PDF Generation" CodeBehind="~/Default.aspx.cs" %>
<html>
<head>
</head>
<body>
<script type="text/javascript" language="JavaScript">
function getHtml() {
var theHtml = document.getElementById('ReportContent').innerHTML;
document.getElementById('<%=hdnHtml.ClientID %>').value = theHtml;
return true;
        }

    </script>
<div id='ReportContent'>    
...Some html content you want turned into a pdf
</div>    
<asp:Button ID="btnSend" OnClick="btnSend_Click" OnClientClick="return getHtml();"
                                                        runat="server" Text="Send" />

</body>
</html>




您还必须发送对css样式表的引用

CS

protected void BtnExport_Click(object sender, EventArgs e)<br/>
{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CreateAndDownloadPDF(this.Request, hdnHtml.Value, Page.ResolveUrl("~/css/MAIN.css"), "NameOfTheFile", "Name of the Report");<br/>
}

public static void CreateAndDownloadPDF(System.Web.HttpRequest ServerRequest, string HTML, string cssfile, string FileName, string Footer)
{
string downloadName = FileName + ".pdf";<br/>
try
{
PdfConverter pdfConverter = new PdfConverter();</br>
pdfConverter.PdfDocumentOptions.PdfPageSize = PdfPageSize.Letter;
pdfConverter.PdfDocumentOptions.FitWidth = false;
pdfConverter.PdfDocumentOptions.PdfCompressionLevel = PdfCompressionLevel.Normal;
pdfConverter.PdfDocumentOptions.ShowFooter = true;
pdfConverter.PdfDocumentOptions.LeftMargin = 25;
pdfConverter.PdfDocumentOptions.RightMargin = 25;
pdfConverter.PdfDocumentOptions.TopMargin = 25;
pdfConverter.PdfDocumentOptions.BottomMargin = 15;
pdfConverter.PdfDocumentOptions.GenerateSelectablePdf = true;
pdfConverter.AvoidImageBreak = true;

pdfConverter.PdfDocumentOptions.ShowHeader = false;

pdfConverter.PdfFooterOptions.FooterText = Footer;
pdfConverter.PdfFooterOptions.FooterTextColor = Color.Black;
pdfConverter.PdfFooterOptions.DrawFooterLine = true;
pdfConverter.PdfFooterOptions.PageNumberText = "Page";
pdfConverter.PdfFooterOptions.ShowPageNumber = true;

pdfConverter.LicenseKey = "LICENSE_KEY_HERE";
string strHTML = "<html><head><link href='" + cssfile + "' rel='stylesheet' type='text/css' /></head><body>" + HTML + "</body></html>";


//set page url
string url = "http://" + ServerRequest.ServerVariables["SERVER_NAME"] + port + ServerRequest.ServerVariables["SCRIPT_NAME"];
//end set page url   
byte[] downloadBytes = pdfConverter.GetPdfBytesFromHtmlString(strHTML, rmsPath);

System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.Clear();
response.AddHeader("Content-Type", "binary/octet-stream");
response.AddHeader("Content-Disposition", "attachment; filename=" + downloadName + "; size=" + downloadBytes.Length.ToString());
response.BinaryWrite(downloadBytes);
response.End();
}
catch (System.Threading.ThreadAbortException) { }  //for response.End()
catch (Exception ex)
{
//Error handling
}
finally
{
if (File.Exists(ServerRequest.MapPath(downloadName)))
     File.Delete(ServerRequest.MapPath(downloadName));
}
}