2

I have a VisualForce page that I need to convert to PDF.

Now the challenge is that this page has a few components constructed with JavaScript (including, but not limited to, jQuery), so 'runAs="pdf"' wouldn't work. So I need to load the page, wait until it gets fully constructed with JS, and then convert it to PDF.

Any ideas on how to get this done, if its doable at all?

6
  • Check out www.spurdoc.com - You will need to make a public site available to display the page but it will create a PDF for you from the final page
    – Eric
    Commented Nov 18, 2015 at 19:06
  • No, absolutely can't make it public. Page contains some sensitive data.
    – user227895
    Commented Nov 18, 2015 at 19:12
  • 1
    You could get creative with a one time limited key in your class and in the controller not show the information unless the URL param key matches what you have. that would control who sees it. I am not saying make the page public per say but it would need to be accessible without standard authentication. Otherwise I do not think what you want to do will be possible if the DOM elements depend on Javascript to insert the elements...Just because you have jQuery on the page does not mean you cannot renderAs pdf, it is what you are doing that will determine that
    – Eric
    Commented Nov 18, 2015 at 19:21
  • Technically it will render, of course, except it will not contain JS-rendered pieces. To test I created a page with single <span> and a jquery code that adds text inside. PDF comes out blank, so I would assume it renders PDF on server side and does not run JS before rendering. (I hope I'm wrong, btw...).
    – user227895
    Commented Nov 18, 2015 at 19:29
  • your not wrong...
    – Eric
    Commented Nov 18, 2015 at 19:30

1 Answer 1

1

As you mentioned, since you want the PDF generated after javascript has executed, renderAs="pdf" is off the table.

You will probably need to use a separate server running a headless browser such as PhantomJS. Phantom is capable of printing a page as a pdf, using that capability along with the Salesforce frontdoor login you can create a web service on your server which is called from your visualforce page. This service would login to salesforce via frontdoor login, redirect to the page you want as a pdf, and then phantom could render it out as a pdf after the javascript has executed.

Beware that session ids generated from visualforce don't work with the frontdoor login, so your web service is going to need to obtain a session id through some means other than passing it in. You would probably need to find a method of verifying that the PDF requested should be visible to the person requesting it.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .