[SalesForce] Render a Visualforce page as PDF issues

I'm trying to render a HTML table in a Visualvorce page as PDF and I'm having the following issues:

  1. I used the CSS below to display the page in landscape but it doesn't work.

@page {
size:landscape;
}

  1. When rendering the VF page as PDF, the table is displayed without the colors I added using CSS.

  2. How can I create a button that shows the VF page ready to be printed out?

Thank you very much!

Best Answer

The PDF renderer's support of CSS Is limited and some trial and error is nearly always required to get the results you want.

I'm not sure why your colours don't work, I've had no issues with that in the past so you might need to post the CSS and markup to show how you're doing it. The code you're using for landscape is correct, so I'm lead to think that you might have a syntax error somewhere that's breaking everything below a certain point.

Finally, you could open the PDF In an iFrame or similar, but since different operating systems and browser combinations handle things very differently I'd be inclined to just open the page in a new tab and let the user handle it how they'd like to. For instance on my Mac, Firefox asks me to save PDFs or open them in Preview (a local app) whereas Chrome displays PDFs in the browser window.

Update

The following CSS was being used:

table tr:nth-child(4n+2) { background: #ccc; }

Which while valid, is not supported by the PDF renderer. The solution is to apply the selection logic via Visualforce/Apex, using the styleClass parameter to specify a CSS class for the desired rows.

Related Topic