Welcome Guest Search | Active Topics | Sign In | Register

EO.PDF CSS stylings and color lost using RenderAsPDF in .Net Web Application Options
Li Ding
Posted: Friday, September 30, 2016 1:27:20 PM
Rank: Newbie
Groups: Member

Joined: 5/7/2013
Posts: 8
Hi,

I just installed the EO.PDF and EO.Web from EO.Total 2016. I used it to convert an ASPX page to PDF document.
PDF document is generated with good contents but the CSS styling lost (font color, style, background color, etc).

Here are some related source codes and hope someone could shed lights on how to get the CSS in PDF. If I remove ASPXToPDF1.RenderAsPDF and the component, the CSS comes back in the HTML page. The CSS does not work in PDF well.

In Page_Load: ASPXToPDF1.RenderAsPDF(true);

protected void ASPXToPDF1_AfterRender(object sender, EventArgs e)
{

HtmlToPdfResult result = (HtmlToPdfResult)ASPXToPDF1.Result;
PdfDocument Doc = result.PdfDocument;

HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.ClearHeaders();
response.ContentType = "application/pdf";

Doc.Save(response.OutputStream);
}

Thanks.
eo_support
Posted: Friday, September 30, 2016 9:29:17 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,072
Hi,

Please handle BeforeRender event and check HtmlToPdf.Options.BaseUrl in that event. Usually this occurs when the automatically detected BaseUrl is wrong. Also do you have port number in your Url?

Thanks!
Li Ding
Posted: Friday, November 4, 2016 2:24:25 PM
Rank: Newbie
Groups: Member

Joined: 5/7/2013
Posts: 8
Hi EO Support,

Yes, I handled BeforeRender and set the HtmlToPdf.Options.BaseUrl to be the root directory of the page. On the localhost, there is a port number in the URL http://localhost:51061/ReviewPrintable.aspx; on production the URL won't have a port number, and it will be https://appname.xxx.org/ReviewPrintable.aspx

I noticed that if I removed ASPXTOPDF object from the aspx page and commented out ASPXToPDF1.RenderAsPDF in the cs page, the web page will link to the base CSS file. If I added the ASPXTOPDF object back, the web page is converted to PDF page with images but not font style, background color from the original CSS.

Using Chrome View Page Source, the web page without ASPXToPDF shows HTML, Head, CSS file name etc; the PDF page without ASPXToPDF has EO style elements but not HTML, HEAD or original CSS link elements. It looks to me EO.PDF style built-in the ASPXToPDF object overrides the original CSS file.

Here is the Object in the .aspx page:
<eo:ASPXToPDF ID="ASPXToPDF1" runat="server" OnBeforeRender="ASPXToPDF1_BeforeRender" OnAfterRender="ASPXToPDF1_AfterRender">
</eo:ASPXToPDF>

In the ASPXToPDF1_BeforeRender(object sender, EventArgs e), I tested both root directory and the subdirectory where CSS file locates. Neither worked for me.

HtmlToPdf.Options.BaseUrl = "http://localhost:51061/"
or
HtmlToPdf.Options.BaseUrl = "http://localhost:51061/templates/"

Are there anything else that I can try to get the CSS to work in PDF? Thanks!

eo_support
Posted: Saturday, November 5, 2016 6:31:54 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,072
Hi,

ASPXToPDF does not override your CSS link elements.

When you say it loses style, do you mean it loses style on your local machine or on the production server?

Thanks!
Li Ding
Posted: Monday, November 7, 2016 2:13:55 PM
Rank: Newbie
Groups: Member

Joined: 5/7/2013
Posts: 8
Li Ding
Posted: Monday, November 7, 2016 2:14:56 PM
Rank: Newbie
Groups: Member

Joined: 5/7/2013
Posts: 8
Good to know that ASPXToPDF does not override CSS link elements by design.

In my case, the web Page successfully generates a PDF with correct contents and layout without font style, font size, background colors defined in my own CSS file. The original CSS loses control over the produced PDF. If I removed the EO.PDF object/functions and left everything else unchanged, the aspx web page resumes the style defined in the CSS.

It is the same on both my local machine and on the production server. I changed the HtmlToPdf.Options.BaseUrl value to be either local directory or server directory. I tested the viewing of the PDF on both Chrome and Internet Explorer and the results is the same.

Thanks.

Li
eo_support
Posted: Monday, November 7, 2016 2:55:44 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,072
Hi,

In that case you can try to isolate the problem into a test project and send the test project to us. See here for more details:

https://www.essentialobjects.com/forum/test_project.aspx

Thanks!
Li Ding
Posted: Tuesday, November 8, 2016 4:10:36 PM
Rank: Newbie
Groups: Member

Joined: 5/7/2013
Posts: 8
I emailed the test project to support. Thanks!
eo_support
Posted: Thursday, November 10, 2016 1:15:29 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,072
Hi,

We have looked into the file you sent to us. Please use either one of the following option to fix the issue:

1. Remove server code rendering block in your page header. In your case the "<%=Session["SiteName"]%>" block;

2. Move your CSS file link element into your form element;

The root of the problem is internally we use System.Web.UI.Page.RenderControl to render the page and this method is not capable of rendering the header if the header contains server code block. As such when you have the server code block, the whole header section will not appear in the result HTML file to be rendered, since your CSS link element is in the header element, this would cause the result file missing CSS link. Either remove the server rendering block in your header or move the CSS out of the header (it must be moved to inside of the form) will resolve the issue for you.

Thanks!
Li Ding
Posted: Thursday, November 10, 2016 1:54:51 PM
Rank: Newbie
Groups: Member

Joined: 5/7/2013
Posts: 8
I removed the server block in the header and the problem was solved right away. Awesome! Thanks a lot for your support.
eo_support
Posted: Thursday, November 10, 2016 1:56:27 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,072
Great! Thanks for confirming that it works!


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.