|
Rank: Member Groups: Member
Joined: 4/25/2013 Posts: 17
|
What I would like to do: Generate a pdf of a webpage and attach to an email to be sent. The problem: I cannot change the background of the pdf without changing the background of the webpage. Here's some code that I tried:
Code: C#
protected void btn_Export_to_PDF_Click(object sender, EventArgs e)
{
//Set margins to 0.5 inch on all sides
EO.Pdf.HtmlToPdf.Options.OutputArea = new System.Drawing.RectangleF(0.5f, 0.5f, 7.5f, 10f);
//Set output page size to 8.5 by 11 inches
EO.Pdf.HtmlToPdf.Options.PageSize = new System.Drawing.SizeF(8.5f, 11f);
//Specify Items to Show
EO.Pdf.HtmlToPdf.Options.VisibleElementIds = Panel_Report.ClientID.ToString();
//Specify Items in the Visible Area to Hide
EO.Pdf.HtmlToPdf.Options.InvisibleElementIds = Panel_Export_Options.ClientID.ToString();
//Remove Background From MasterPage
Master.BodyTag.Attributes.Add("style", "background:none; background-color:#fff;");
//Render This Page as a PDF
EO_PDF.RenderAsPDF(false);
}
protected void EO_PDF_AfterRender(object sender, EventArgs e)
{
//Create Memory Stream
MemoryStream PDF_Memory_Stream = new MemoryStream();
//Get the conversion result
EO.Pdf.HtmlToPdfResult result = (EO.Pdf.HtmlToPdfResult)EO_PDF.Result;
//Begin Building Email Info
eeds.Transfer_Object.Email_Info Email_Info = new eeds.Transfer_Object.Email_Info();
Email_Info.From_Address = "email@domainname.com";
Email_Info.To_Address = "email2@@domainname.com";
Email_Info.Subject = "Testing Essential Objects";
//Build Attachment Info
Email_Info.Attachment_Info = new eeds.Transfer_Object.Email_Attachment_Info();
//Save the Pdf Document into the Memory Stram
result.PdfDocument.Save(PDF_Memory_Stream);
//Set Content Type Info
System.Net.Mime.ContentType Content_Type = new System.Net.Mime.ContentType();
Content_Type.MediaType = System.Net.Mime.MediaTypeNames.Application.Pdf;
Content_Type.Name = "Test PDF.pdf";
//Assign Memory Stream to Attachment Info
Email_Info.Attachment_Info.Memory_Stream = PDF_Memory_Stream;
//Assign Content Type to Attachment Info
Email_Info.Attachment_Info.Content_Type = Content_Type;
//Call function to send the email
Email_Info.Send_Email();
}
Previously, I have used the Aspx to PDF control with sendToClient set to true and was able to modify the background of the PDF without updating the web page background. Any insight would be greatly appreciated.
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,196
|
Hi,
You always have to change the web page in order to change the result PDF. The basic rule for our PDF converter is that it renders everything exactly the same as a browser renders it on screen (with a few exceptions, particularly those related to paging). So in order to change output, you always change the input. There is no option on the converter to add a background for you.
While you can not control whether to change your web page or not, you can control when. For example, if you want the page to show up as blue on screen but as green in PDF file, then you change your web page to blue when you are not render to PDF and change your web page to green when you are rendering to PDF.
Your code that handles AfterRender event and sends out email looks fine to us. That has nothing to do with how the PDF is rendered since at that stage the conversion is already done.
Hope this clears up. If you still have any questions please feel free to ask.
Thanks!
|
|
Rank: Member Groups: Member
Joined: 4/25/2013 Posts: 17
|
Just to clarify, I have a background on the webpage, and I do not want a background in the PDF. For example, this is the code I use when I want to have a save/open prompt displayed ("sendToClient" = true):
Code: C#
protected void btn_Export_to_PDF_Click(object sender, EventArgs e)
{
//Set margins to 0.5 inch on all sides
EO.Pdf.HtmlToPdf.Options.OutputArea = new System.Drawing.RectangleF(0.5f, 0.5f, 7.5f, 10f);
//Set output page size to 8.5 by 11 inches
EO.Pdf.HtmlToPdf.Options.PageSize = new System.Drawing.SizeF(8.5f, 11f);
//Specify Items to Show
EO.Pdf.HtmlToPdf.Options.VisibleElementIds = Panel_Report.ClientID.ToString();
//Specify Items in the Visible Area to Hide
EO.Pdf.HtmlToPdf.Options.InvisibleElementIds = Panel_Export_Options.ClientID.ToString();
//Remove Background From MasterPage
Master.BodyTag.Attributes.Add("style", "background:none; background-color:#fff;");
//Render This Page as a PDF -- THIS DOES SEND TO CLIENT
EO_PDF.RenderAsPDF();
}
In this scenario, the background in the PDF is white, and the webpage background does not change. This is what I would like to see happen when "sendToClient" = false.
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,196
|
Hi,
Try to comment out the last line and see what happens on your screen. If you see a background on screen in that case, you will see a background in PDF. If you do not see a background on screen, then you should not see a background in your PDF. You can consider "RenderAsPDF" call as a redirector that redirects everything that would go to your screen to the PDF file. As such to troubleshoot such problem you always turn the "redirector" off and check the output on screen first. This is true regardless your SendToClient settings.
Thanks!
|
|