First steps with PLPDF Hello World!
How does the PDF generating PL/PDF procedure look like? How can you construct the simplest PL/PDF method with just a few lines of code?
PL/PDF is implemented in native Oracle PL/SQL that’s the reason why:
- you only need to know PL/SQL
- every known PL/SQL behavior applies to PL/PDF too (owner, rights, access to data etc.)
- it can be integrated into any kind of PL/SQL processing method or application (e.g. Apex)
Therefore, all of the syntax in this post is going to be pure PL/SQL, nothing else. Using PL/PDF means using procedures and functions from PL/PDF packages in order to create PL/SQL code which output is a PDF file or binary value in the form of BLOB. To work with PL/PDF just open a PL/SQL developer tool of your choice and contact to a database which already has PL/PDF installed in it and you're set.
The starting and the ending point
All of the starts and ends of PL/PDF procedures are fix. The start is always a plpdf.init and the end is a plpdf.SendDoc. The magic happens between these two commands: the construction of the content of your PDF. The plpdf.init, as its name implies, is used to indicate that we are starting to build up a new PDF file. After this command PL/PDF resets all of its relevant inside variables and structures and also stores the variables given in the init for using them latter when executing the program. The end of the PL/PDF method is always the plpdf.SendDoc which creates the PDF itself and returns it in a BLOB out parameter. When it’s done, all we need to do is storing it in an ordinary Oracle table. However, one can also get the outcome in a browser through webserver and using Oracle Web Agent (OWA).
This is how the process’ frame looks like:
create or replace procedure helloworld is l_pdf blob; begin plpdf.init(); plpdf.senddoc(l_pdf); end;
Creating the content of the PDF
Now we have to fill it with the procedures which will make up the content of our PDF directly or indirectly. It’s common knowledge that PDFs are made up by pages, so we better add some if we are planning to write or draw on them. The plpdf.NewPage procedure will help us adding a new page. Every plpdf.NewPage command creates a new page, the content between two of these commands are ending up on the same page. It’s similar to a page break. The last page has to be closed by the already mentioned plpdf.SendDoc.
Let’s see what we have then:
create or replace procedure helloworld is l_pdf blob; begin plpdf.init(); plpdf.newpage(); plpdf.senddoc(l_pdf); end; The next step is writing the proper commands between plpdf.NewPage() and plpdf.SendDoc to fill out the blank page with the content we want. Choose the font properties and put some words on that page!
--Sets the font properties plpdf.SetPrintFont( p_family => 'Arial', -- Font family: Arial p_style => null, -- Font style: Regular p_size => 12 -- Font size: 12 ); --Add text plpdf.PrintoutText( p_x => 20, p_y => 30, p_txt => 'Hello World!' ); The parameters of the procedures used in the printing (“p_x => 20, p_y=>30”) will be discussed in a latter post. If you can’t wait that long, check out the PL/PDF User Guide to get to know more about the procedures and the meaning of their parameters.
This is where we are now:
create or replace procedure helloworld is l_pdf blob; begin plpdf.Init(); plpdf.NewPage(); plpdf.SetPrintFont( p_family => 'Arial', -- Font family: Arial p_style => null, -- Font style: Regular p_size => 12 -- Font size: 12 ); plpdf.PrintoutText( p_x => 20, p_y => 30, p_txt => 'Hello World!' ); plpdf.SendDoc( p_blob => l_pdf ); end; We are almost there! One last decision we have to make is deciding how to manage our brand new PDF. There are two options ahead of us:
- storing it in an Oracle table
- sending down to the client through webserver
So, one of these two should go between the plpdf.SendDoc and the end command.
In case of storing:
insert into STORE_BLOB (blob_file, created_date, filename) VALUES (l_pdf,sysdate, 'helloworld.pdf') commit;
If choosing the webserver solution:
htp.p('Content-Length: ' || dbms_lob.getlength(l_blob));
htp.p('Content-Disposition: inline; filename="helloworld.pdf"');
For the first sight these methods may seem a little complicated, but the related Oracle documentations are describing the operation of OWA and HTP packages pretty well.
For now we chose storing because it can be used from a simple PL/SQL editor and the executing is also very easy:
Finally, the output can be downloaded from the table you used.