Reputation: 4943
I have a web page that uses a combination of gridviews and html elements to create a report. I would like to give the user an option to click and it will export to a pdf. I would prefer not to create the pdf on the server as that requires me to write another process to clean up files.
Optimally, I'd like the current page to open a new page that renders the pdf and prompts the user to save it/open it.
I've looked at iTextSharp and am interested in using it if I don't have to specify every element. If there is a way to specify a panel and all of it's contents or an alternative, I'm open to that too.
Thanks!
Upvotes: 1
Views: 9382
Reputation: 7353
I know this is an old question but my company does something very similar to what you need. We use ExpertPDF to convert the html to pdf and do the following to get the html. It is important that you turn EnableEventValidation="false" as sending back html to the server is a security risk.
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>
You will also have to send a reference to your css style sheet as well
CS
protected void BtnExport_Click(object sender, EventArgs e)<br/>
{
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));
}
}
Upvotes: 1
Reputation: 4769
This would be way way easy if you could convince the user to install a PDF Printer like CutePDf or Foxit PDF Creator
Upvotes: 2