8
Creating PDF Documents with CodeCharge Studio © 2004, Donald N. Bevis for GoToDon.com Permission is granted to distribute freely as long as this copyright notice is retained in it's entirety without modification. Ownership of this document remains with the copyright holder. Improvements or corrections are welcomed. See http://www.GoToDon.Com/ccbth for contact information. This document is available for download from the aforementioned location This document will provide you some insight into creating your own reports, output as PDF files, from CodeCharge pages. Actually, the process works for any URL want to capture. There is a very powerful set of functions available as GPL'd code. Most of what you need will be installed and ready to go if your webserver is Linux. I set this up on a Fedora Core distribution from RedHat and it was extremely easy. Only the outer "wrapper" from http://www.rustyparts.com was missing - everything else is in the Fedora distribution. What do you need? The process described in this document takes HTML from any URL, but in particular, from a CCS "report" page. The HTML is converted into PostScript, then the PostScript is converted into PDF. No Adobe components are used, everything is public domain. The shopping list of "parts" you need: Linux/Unix Apache httpd (bundled with Linux) PEAR library (supports PHP: http://pear.php.com Perl scripting language version 5 (bundled with Linux) GhostScript PS to PDF converter (bundled with Linux) HTML_2PDF PHP class module from http://www.rustyparts.com html2ps perlscript from http://user.it.uu.se/~jan Actually, all of these items are available for use on Windows 2000. I ran into numerous problems trying to correctly define paths to all the scripts and executables. The documentation is quite thin and I was not able to figure out why the PS to PDF convertor would not work from the script - it worked fine on it's own. Rather than fight it, and the intended target server was Linux anyway, I quit trying to get it to work on Windows 2000. HTML_2PDF actually implements several options for inputting HTML. I used the "from an external URL" method, which meant I could create the report however I wanted it to look, by designing a page in CCS, then let the HTML_2PDF script scrape that page to get the data. This also means you can direct it at any URL on the WWW and save the page as a PDF file! Hooking it up to CodeCharge Studio Typically a report requires parameters: date ranges, customer number, etc. So it's not enough to just give the script a URL. I wrote a simple report configuration front-end. The reports I needed required a small set of parameters (about 5 different ones). So I created a table to store the report URL as a string, and then added 5 boolean columns to indicate which parameters were needed. The Report form contains 5 sets of data fields, and these are either hidden or visible depending on whether the report needs them. Users run reports by selecting the name, This causes the appropriate list of parameters to be requested. Code in the "Submit" button's Server OnClick event. This event assembles the parameters from the form, instantiates and invokes the object implemented by HTML_2PDF. This creates the report and stores it in a folder on the webserver. A table stores report descriptions and the URL where the report was filed. This let's users retrieve them via their web browser. Here's a screen shot of the report interface for users.

Creating PDFs With Code Charge Studio

Embed Size (px)

Citation preview

Page 1: Creating PDFs With Code Charge Studio

Creating PDF Documents with CodeCharge Studio

© 2004, Donald N. Bevis for GoToDon.com

Permission is granted to distribute freely as long as this copyright notice is retained in it's entire ty without modification. Ownership of this document r emains with the copyright holder. Improvements or corrections are welcomed. See http://www.GoToDon.C om/ccbth for contact information. This document is available for download from the aforementioned loca tion

This document will provide you some insight into creating your own reports, output as PDF files, from CodeCharge pages. Actually, the process works for any URL want to capture.

There is a very powerful set of functions available as GPL'd code. Most of what you need will be installed and ready to go if your webserver is Linux. I set this up on a Fedora Core distribution from RedHat and it was extremely easy. Only the outer "wrapper" from http://www.rustyparts.com was missing - everything else is in the Fedora distribution.

What do you need? The process described in this document takes HTML from any URL, but in particular, from a CCS "report" page. The HTML is converted into PostScript, then the PostScript is converted into PDF. No Adobe components are used, everything is public domain.

The shopping list of "parts" you need:

Linux/Unix

Apache httpd (bundled with Linux)

PEAR library (supports PHP: http://pear.php.com

Perl scripting language version 5 (bundled with Linux)

GhostScript PS to PDF converter (bundled with Linux)

HTML_2PDF PHP class module from http://www.rustyparts.com

html2ps perlscript from http://user.it.uu.se/~jan

Actually, all of these items are available for use on Windows 2000. I ran into numerous problems trying to correctly define paths to all the scripts and executables. The documentation is quite thin and I was not able to figure out why the PS to PDF convertor would not work from the script - it worked fine on it's own. Rather than fight it, and the intended target server was Linux anyway, I quit trying to get it to work on Windows 2000.

HTML_2PDF actually implements several options for inputting HTML. I used the "from an external URL" method, which meant I could create the report however I wanted it to look, by designing a page in CCS, then let the HTML_2PDF script scrape that page to get the data. This also means you can direct it at any URL on the WWW and save the page as a PDF file!

Hooking it up to CodeCharge Studio Typically a report requires parameters: date ranges, customer number, etc. So it's not enough to just give the script a URL. I wrote a simple report configuration front-end. The reports I needed required a small set of parameters (about 5 different ones). So I created a table to store the report URL as a string, and then added 5 boolean columns to indicate which parameters were needed. The Report form contains 5 sets of data fields, and these are either hidden or visible depending on whether the report needs them.

Users run reports by selecting the name, This causes the appropriate list of parameters to be requested. Code in the "Submit" button's Server OnClick event. This event assembles the parameters from the form, instantiates and invokes the object implemented by HTML_2PDF. This creates the report and stores it in a folder on the webserver. A table stores report descriptions and the URL where the report was filed. This let's users retrieve them via their web browser.

Here's a screen shot of the report interface for users.

Page 2: Creating PDFs With Code Charge Studio

Creating PDF Documents with CodeCharge Studio

© 2004, Donald N. Bevis for GoToDon.com

Permission is granted to distribute freely as long as this copyright notice is retained in it's entire ty without modification. Ownership of this document r emains with the copyright holder. Improvements or corrections are welcomed. See http://www.GoToDon.C om/ccbth for contact information. This document is available for download from the aforementioned loca tion

The code called by the Run Report button is shown below. It is mostly unchanged from the example provided at rustyparts.com. I just pasted it into the event code-file for my report page.

Creating the Reports This is the really easy part. The reports have to conform to the above-listed parameters (at least thr my application). Beyond that, the report is anything you want it to be. I created reports as regular CCS pages with grids or record controls. The one thing to remember is that you can't use a Navigator to paginate your report - it has to be output in one continuous stream. So page breaks are not going to be automatically inserted. This is a simple report mechanism. I probably would use a dedicated report-generator if I wanted really sophisticated output, with headers, footers, pagination, grouping and so on. But for simple things like printing invoices or other small reports, it is a good fit. You can put code into the Before Show Row event to insert a page break and/or header every "n" lines and do a reasonably good job of pagination.

By default the conversion script converts everything in the specified URL - including buttons, textboxes or other controls. I edited my copy of the script in a couple places to not render the outlines of textboxes and to skip buttons. This made a better looking report for my application.

Report-execution function called from Event handler When setting up the PDF conversion, it is helpful to set $pdf->setDebug(true); to have t display on-screen all the conversion steps and their final outcomes.

Page 3: Creating PDFs With Code Charge Studio

Creating PDF Documents with CodeCharge Studio

© 2004, Donald N. Bevis for GoToDon.com

Permission is granted to distribute freely as long as this copyright notice is retained in it's entire ty without modification. Ownership of this document r emains with the copyright holder. Improvements or corrections are welcomed. See http://www.GoToDon.C om/ccbth for contact information. This document is available for download from the aforementioned loca tion

// Invoke the HTML_2PDF conversion obtained from http://www.rustyparts.com

function run_report($pdfFile, $htmlFile, $defaultDo main)

{

// require the class

require_once dirname('__FILE__') .'/reports/pdf/HTM L_ToPDF-custom.php';

// remove old one, just to make sure we are making it afresh

@unlink($pdfFile);

$pdf =& new HTML_ToPDF($htmlFile, $defaultDomain, $ pdfFile);

$pdf->setTmpDir(dirname('__FILE__') .'/reports/pdf/ tmp/');

// Control the output of additional debug informati on

$pdf->setDebug(false); // When 'true' this dumps t racing info to the browser while the conversion to PDF executes

// set that we do want to use the page's css

$pdf->setUseCSS(true);

// give it our own css, in this case it will make i t so

// the lines are double spaced

$pdf->setAdditionalCSS('

p {

line-height: 1.8em;

font-size: 12pt;

}');

// don't want to underline hyperlinks

$pdf->setUnderlineLinks(false);

// scale the page down slightly, just seems to fit the page better

$pdf->setScaleFactor('.9');

// make the page black and light

$pdf->setUseColor(false);

Page 4: Creating PDFs With Code Charge Studio

Creating PDF Documents with CodeCharge Studio

© 2004, Donald N. Bevis for GoToDon.com

Permission is granted to distribute freely as long as this copyright notice is retained in it's entire ty without modification. Ownership of this document r emains with the copyright holder. Improvements or corrections are welcomed. See http://www.GoToDon.C om/ccbth for contact information. This document is available for download from the aforementioned loca tion

// convert the HTML file to PDF

$result = $pdf->convert();

// PDFs are created and owned by "apache", let the users access them by this chmod, chown

if (file_exists($pdfFile)) {

chmod($pdfFile, 0777);

// chown($pdfFile, 'nobody');

}

// check if the result was an error

return PEAR::isError($result);

}

Event code for the "Run Report" button This simply gathers data from the form, assembles the complete URL for running the report, generates a filename (in this instance a path to the folder "uploads". The application incorporated a fileupload function and so reports were designed to be stored in the same folder and listed in the same "directory" (CCS grid) with all the uploaded files.

Page 5: Creating PDFs With Code Charge Studio

Creating PDF Documents with CodeCharge Studio

© 2004, Donald N. Bevis for GoToDon.com

Permission is granted to distribute freely as long as this copyright notice is retained in it's entire ty without modification. Ownership of this document r emains with the copyright holder. Improvements or corrections are welcomed. See http://www.GoToDon.C om/ccbth for contact information. This document is available for download from the aforementioned loca tion

//reports_Button_Run_OnClick @19-E856C3FF

function reports_Button_Run_OnClick()

{

$reports_Button_Run_OnClick = true;

//End reports_Button_Run_OnClick

//Custom Code @33-A43F7AC2

// -------------------------

global $reports;

global $Redirect;

//

$hostdomain = "localhost"; // Change this if the reports are not on a different server

// Invoke the report-generator object to produce th e PDF copy of our report URL

// The list of POST parameters here must correspond to each of the form fields that accept user input

// By convention, all .php report files are designe d to recognize parameters

// by the names cid, pid, yr,st and en

$sep = "?";

$temp = CCGetFromPost("cid","0");

if ($temp !== "0") {

$params = $params . $sep . "cid=" . $temp;

$sep = "&";

}

$temp = CCGetFromPost("List_Ppty","0");

if ($temp !== "0") {

$params = $params . "&pid=" . $temp;

$sep = "&";

}

Page 6: Creating PDFs With Code Charge Studio

Creating PDF Documents with CodeCharge Studio

© 2004, Donald N. Bevis for GoToDon.com

Permission is granted to distribute freely as long as this copyright notice is retained in it's entire ty without modification. Ownership of this document r emains with the copyright holder. Improvements or corrections are welcomed. See http://www.GoToDon.C om/ccbth for contact information. This document is available for download from the aforementioned loca tion

$temp = CCGetFromPost("Text_Year","0000");

if ($temp !== "0") {

$params = $params . "&yr=" . $temp;

$sep = "&";

}

$temp = CCGetFromPost("Text_From","0000");

if ($temp !== "0") {

$params = $params . "&st=" . $temp;

$sep = "&";

}

$temp = CCGetFromPost("Text_To","0000");

if ($temp !== "0") {

$params = $params . "&en=" . $temp;

$sep = "&";

}

// Determine where the output file will be stored

$rptpath = "/uploads/" . CCGetParam("cid","0") . " /";

// Create the output filename based on the reportn ame field

$filename = $reports->reportname->GetValue() . ".p df";

// Ensure the file does not exist, append a number to it until we create a unique file name

// This is the same algorithm as used by the FileU pload class

$file_exists = true;

$index = 0;

while($file_exists) {

$ActualFileName = dirname(__FILE__) . $rptpath . date("YmdHis") . $index . "." . $filename;

Page 7: Creating PDFs With Code Charge Studio

Creating PDF Documents with CodeCharge Studio

© 2004, Donald N. Bevis for GoToDon.com

Permission is granted to distribute freely as long as this copyright notice is retained in it's entire ty without modification. Ownership of this document r emains with the copyright holder. Improvements or corrections are welcomed. See http://www.GoToDon.C om/ccbth for contact information. This document is available for download from the aforementioned loca tion

$last_report_URL = ServerURL . $rptpath . date("Y mdHis") . $index . "." . $filename;

$last_report_file = date("YmdHis") . $index . "." . $filename;

$file_exists = file_exists($ActualFileName);

$index++;

}

$output_file = $ActualFileName;

$report_URL = $reports->new_URL->GetValue();

// Construction the URL of the report to be run by combining the URI of the report with the query parameters

if (strcasecmp(substr($report_URL,1,5),"http:") == 0) {

}

else {

// Construct a URL from various parts

if (substr($report_URL,1,1) !== "/") {

$report_URL = "/" . $report_URL;

}

$report_URL = ServerURL . "reports" . $report_URL . ".php" . $params;

}

// Produce the report

run_report($output_file, $report_URL, $hostdomain) ;

//

// Determine whether the report ran correctly or no t

if (file_exists($output_file)) {

// Temporarily store the filename and callback thi s php page with a parameter so it displays the "reportlog" form

CCSetSession("last_report_path",$output_file);

CCSetSession("last_report_URL",$last_report_URL);

Page 8: Creating PDFs With Code Charge Studio

Creating PDF Documents with CodeCharge Studio

© 2004, Donald N. Bevis for GoToDon.com

Permission is granted to distribute freely as long as this copyright notice is retained in it's entire ty without modification. Ownership of this document r emains with the copyright holder. Improvements or corrections are welcomed. See http://www.GoToDon.C om/ccbth for contact information. This document is available for download from the aforementioned loca tion

CCSetSession("last_report_file",$last_report_file );

$Redirect = $Redirect . "&rpt=1";

}

else {

echo "<br>File was not created<br>";

}

// -------------------------

//End Custom Code

//Close reports_Button_Run_OnClick @19-F550C3E4

return $reports_Button_Run_OnClick;

}

//End Close reports_Button_Run_OnClick