Monday, October 12, 2009

How to integrate PL_FPDF with Apex

In my previous post about the free PL_FPDF package that allows you to produce PDF documents from PL/SQL, there was a comment asking for a step-by-step guide on how to integrate this with Oracle Application Express (Apex).

This is really quite simple. By default, the pl_fpdf.output procedure will "print" your PDF document to the web browser along with a header that instructs the browser that this is a downloadable document. All you need to do is to call your procedure that generates the PDF document from a page process within Apex.

Here are the steps:

1. Start by compiling the demo procedure from my previous post into the parsing schema of your Apex application.

2. In Apex, go to your application and add a new, blank page. Let's assume the new page number is 4.

3. Under the "Page Rendering" section, add a new Process of type "PL/SQL". The name of the process can be anything, but let's call it "Produce PDF". Make sure that the point is "On load - before header".



4. In the process source, add the following PL/SQL code:


begin

  -- call the procedure to generate the PDF document and send it to the browser
  test_pl_fpdf;

  -- stop the Apex engine from running the rest of the page
  apex_application.g_unrecoverable_error := true;

end;


5. Add a link to the new page (page number 4 if you follow my assumption in step 2) from any other page in the application.

6. Run the application. Now, whenever you navigate to page 4 the PDF file will be downloaded to the browser.

That's all you need to integrate PDF generation into your Apex application.

3 comments:

Anonymous said...

Indeed, very simple... if you know how. And now I know how. Thank you very much for these instructions.

Anonymous said...

Hello,

Awesome tutorials. I was just wondering how would you do this all on-demand?

The default return from Output is 'I' (browser) -> htp.prn

I need to send some data/information on the fly via an ajax call. The response is a cryptic %PDF .....

Could one simply do a javascript window.open, window.write(responseFromFpdfHtpPrn)?

Aside from that, you examples are great and have greatly assisted.

J.

Morten Braten said...

@J:

As you have seen, you can't just take the PDF output and dump it on the page; you need to tell the browser that this is a file download.

That said, I don't really see the need for Ajax here... why won't a simple HTML link (that opens either in the same window or in a popup window) to the page that generates the download suffice?

You can pass data/information via parameters in the URL, as for any Apex page (and if you need to do it dynamically you can always generate/modify the link using Javascript).

- Morten