Satoshi's Data Labo            
Development Memo : Output PDF Report from cloud DB system on Rental Web Server

Recently I have been developing basic MySQL based DB systems on the rental web server.
In the developments, I created very simple reports which just display query results, and never tried to output complex reports like goverment-designated forms. Now I'm going to analyze the way to output query results into PDF Forms. I just write it down as my personal reference per below.

I happened to realize there were some foreign visitors browsing my Japanese development memo, then I translated it to English...

The original target was; get and/or create PDF Form and mapping the query results to each field on the PDF Form.  According to my quick research, I found iText must be a popular solution.  But unfortunately, iText is written by JAVA (in case of iText Sharp, written by C#), therefore it doesn't run on my rental web server.

Next, considering the constraint of the rental server, I picked up two libraries, which are written by Java Script or by PHP, so that it will run even on the rental server. 

① jsPDF (1.3.2)
I firstly tried jsPDF, as it is said jsPDF can handle PDF Form.
Download Zip file, extract and save it under the root folder as jsPDF-1.3.2 folder.
Also create another folder named pdf_test folder, in which I saved sample files. 

The result of test are as shown per below, I can call Java Script from the html file and create PDF file easily.
Unfortunately, according to the examples on the official web site, jsPDF can generate PDF Form itself (togher with contents of the fields),  but it can't fill-in the query results into the target fields.


/PDF01.png

< test.html >  -->  test.html

/PDF03.png

< basic.js >

/PDF04.png


② TCPDF & FPDI
Originally, I am aware that TCPDF is the most popular PDF output library, but it is said TCPDF can't fill-in PDF Forms, that's the reason why I tried to find other PDF output libraries.  Anyway, I come back to the original place.
As I had no chance to use TCPDF before, I confirmed functions of TCPDF step by step per below.

Firstly save TCPDF library under the root folder.  
Secondly save FPDI library (including FPDF-TPL.php) under the root folder as well.  
Thirdly create pdf_test2 folder, which contains PDF template file (Book1.pdf) and other test files.

※ PDF template file shall be saved as PDF Ver.1.4 format.  FPDI can't handle newer versions of PDF files.  In my case, I installed CutePDF, which enable to select PDF format.  Technically, we can change the setting of Acrobat Distiller and control the PDF format.  But change of setting naturally affects to the all PDF files to be output.


/PDF02.png

< Book1.pdf >

/PDF05.png

< test2.php >  -->  test2.php
Output texts into the MultiCell on the PDF template.

/PDF06a.png


< test3.php >  -->  test3.php
Basically same as Test 2, just output variables instead of texts.

/PDF07.png

< test4.php >  -->  test4.php
Instead of MultiCell, using Write method.

/PDF08.png

< test5.php >  -->  test5.php
Query from MySQL DB by database.php, then output the quesry result as table.

/PDF09.png

< database.php >

/PDF11.png

< test6.php >  -->  test6.php
Define the array, output items of array page by page.

/PDF10.png

< test7.php >  -->  test7.php
As same as test6, prepare test7.php and new PDF template (Book2.pdf).
Output Title & Auther in book table (prepared for Test5) into the PDF template.

/PDF12.png

Output result: Looks good!

/PDF13.png

For the purpose of setting the XY axis coordinates, display "Cursor Coordinates" in the Acrobat.

/PDF14.png
 
 
 
Copyright © 2017 Satoshi Ashikari All Rights Reserved.