I want to convert a Visualforce to Pdf and preserve the visualforce css styling the same time in the generated PDF. I am able to achieve the first part using the attribute renderas='pdf'
. However the styles are getting lost. Any pointers if and how both can be achieved at the same time.
Best Answer
When we add
renderas='pdf'
then Salesforce create PDF in server side in that process we lost many CSS which are not compatible with Salesforce's PDF engine.You can try
renderAs="advanced_pdf"
to render in PDF. It support much better CSS but also have some limitation. You can read them in details here: Use Advanced PDF to Render Visualforce Pages as PDF Files (Pilot).Another thing which you can do is
renderas='{!DynamicType}'
you can also pass a dynamic type(HTML or PDF) here and based on your use case display the rendered PDF or a normal Visualforce page.