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.

11 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

John Locke said...

Hi Morthen, this is Eduardo, I work for Oracle on APEX projects, this is really what I was looking for. I was wondering if you could help me. I need to merge (after some manipulation) two or more pdf's within APEX using PLSQL. Is it possible to od it with this library? or perhaps by reading the content and then generating a single pdf instance with all the pdf's appended or merged into one single output?

Morten Braten said...

@Eduardo: Take a look at the commercial PL/PDF library, it has a separate toolkit that allows you to merge existing PDF documents: http://plpdf.com/plpdf-toolkit.html

- Morten

Anonymous said...

Hi Morten,

Good Tutorial, really good, its very easy to implement after reading your post.
Just one question for you, I am having one requirement where I need to add chart into the pdf, and that's also dynamically generated from database data. Is this possible with this package?

Tauceef

Morten Braten said...

@Tauceef: The PL_FPDF package can add images from a URL. So if you create a procedure in the database that generates a JPG or PNG, and then expose that as a URL, then that should work.

- Morten

Raphael Nunes said...

Does anyone know if there is a way to merge multiples pdf into a single one through this package or any other?
Thanks.

Morten Braten said...

@Raphael: The PL/PDF Toolkit (see http://plpdf.com/plpdf-toolkit.html) can merge multiple PDF documents into one, as well as extract pages from existing PDFs, and more.

- Morten

Raphael Nunes said...

@Morten
Thank you for the reply, but I'm needing a free solution. Don't you know if is it possible with this PL_FPDF?

Thank you again.

JCG said...

i downloaded the pdf_builder_pkg, and it works nice. However, when i call the ref2cursor procedure and i use a sum function in the query, the column with the sum query is not printed out on the pdf. Have you run into this?