jjrjr1
Posts: 942
|
| Posted: 02/04/2008, 3:12 AM |
|
Hi
If anyone is interested I have found a great way to generate PDF documents in CCS.
You basically generate a page in CSS, format it anyway you want with any data (Just like you would want it to kook oin HTML), the either display the page in the browser or create a PDF. This allows you to create PDFs just by creating any page formatted any way you want.
If anyone is iterested in this, just reply to this post and I will share it with you.

John
_________________
John Real - More CodeCharge Studio Support at - http://CCSElite.com |
 |
 |
worker73
Posts: 3
|
| Posted: 02/04/2008, 3:16 AM |
|
Hey, Sounds Great, where can i take a look on it?
_________________
We create Worlds
http://www.webernig.com/ |
 |
 |
Gena
Posts: 591
|
| Posted: 02/04/2008, 3:21 AM |
|
Sure! please share!
_________________
Gena |
 |
 |
smokingguns
Posts: 51
|
| Posted: 02/04/2008, 6:29 AM |
|
Awesome..
|
 |
 |
Zye
Posts: 56
|
| Posted: 02/04/2008, 7:23 AM |
|
Yay! I'm down for that. Tell me more if you please
|
 |
 |
yoleonard
Posts: 6
|
| Posted: 02/04/2008, 8:40 AM |
|
Please send me the info too.
Thx.
_________________
Rogmo |
 |
 |
Alvaro Cobo
|
| Posted: 02/04/2008, 9:13 AM |
|
Hi:
I also would like to get the info.
Thanks,
Alvaro
jjrjr1 escribió:
> Hi
>
> If anyone is interested I have found a great way to generate PDF documents in
> CCS.
>
> You basically generate a page in CSS, format it anyway you want with any data
> (Just like you would want it to kook oin HTML), the either display the page in
> the browser or create a PDF. This allows you to create PDFs just by creating
> any page formatted any way you want.
>
> If anyone is iterested in this, just reply to this post and I will share it
> with you.
>
> 
> John
> _________________
> Cheers
> John
> ---------------------------------------
> Sent from YesSoftware forum
> http://forums.codecharge.com/
>
|
|
|
 |
DonB
|
| Posted: 02/04/2008, 9:59 AM |
|
Are you using the curl library (or with later PHP versions, a
file_get_contents() function call) to fetch the page, and fpdf to transform
it into PDF? If so, that works pretty well, but (a) doesn't observe page
breaks and (2) might trim the edges off if you aren't careful (margins set
incorrectly).
--
DonB
http://ccswiki.gotodon.net
"jjrjr1" <jjrjr1@forum.codecharge> wrote in message
news:547a6f31116211@news.codecharge.com...
> Hi
>
> If anyone is interested I have found a great way to generate PDF documents
> in
> CCS.
>
> You basically generate a page in CSS, format it anyway you want with any
> data
> (Just like you would want it to kook oin HTML), the either display the
> page in
> the browser or create a PDF. This allows you to create PDFs just by
> creating
> any page formatted any way you want.
>
> If anyone is iterested in this, just reply to this post and I will share
> it
> with you.
>
> 
> John
> _________________
> Cheers
> John
> ---------------------------------------
> Sent from YesSoftware forum
> http://forums.codecharge.com/
>
|
|
|
 |
ckroon
Posts: 869
|
| Posted: 02/04/2008, 10:49 AM |
|
Hook me up with that too please!
_________________
Walter Kempees...you are dearly missed. |
 |
 |
jjrjr1
Posts: 942
|
| Posted: 02/04/2008, 11:47 AM |
|
Hi all
I puzzled over this for a very long time. I explored server side applications (which were very expensive) and several others. It turns out I found a really cool PHP solution to the problem which allows you to use it on many hosting environments including shared servers.
(You cannot run it at Yahoo host or any other hosts where PHP exec and passthru funcions are blocked or where you cannot make a tmp directory write enabled. This is the only restriction I have found.)
First download a PHP application called dompdf.
It can be downloaded at: http://www.digitaljunkies.ca/dompdf/
After you have checked this thing out you will find it pretty good at creating PDFs in many differnt command fassions.
In order to interface it with CCS you need to make some modifications. All the modifications are all in the dompdf.php script. Those modifications are basically to make it capable of running in CGI mode since you will be using the exec function in CCS.
This is not too hard simply copy the case statement where $sapi = cli and make the copy case statement for cgi.
Also since the $_SERVER["SCRIPT_NAME"] value is not available you need to add a -s switch in the options section so CSS can pass that value to dompdf.
Place this code in the dompfd_usage() function:
" -s \tvery verbose: sets SERVER_FILENAME When running in cgi mode\n".
Place this code in the case statement for getoptions() function.
case "-s":
if ( !isset($_SERVER["argv"][$i+1]) )
die("-f switch requires the SCRIPT_FINENAME parameter\n");
$opts["s"] = $_SERVER["argv"][$i+1];
$_SERVER["SCRIPT_FILENAME"]=$_SERVER["argv"][$i+1];
$i += 2;
break;
You are done with the changes for dompdf. Upload all of it to your server (including the directories. You Can omit the sample pages if you want).
The next part is where is tooooo cool and is one of the reasons we use a great tool like CCS. (I have been in soffware development for over 25 years and have never seen such a well designed and implemented tool with more features than you can imageine. I am still finding WOW things in CCS,)
Did you know you have access to the output buffers before a page is served up. Well you do!!And it is called $Main_Block and is avialable to use at the before output event. I am only mentioning this for your future reference. You will see this and figure that out after you take the next step.
At the page level (be sure you are not pointed to a control or form) add event before output and select Convert to PDF. When you look at this function you will see what I mean about $Main_Block. Too Cool....
Anyway you will see the code CCS generates. This code will not work with dompdf and appears to use the YesSoftware pdf converter and also seems to be windows based. I was a little confised about this but oh well. Remove all that code and replace it with what I have pasted below.
It is a good idea to read the documentation is CCS regarding PDF conversion only so you understand what is meant by the _convert parameter. When you create the convert to PDF event leave that at the default of _convert.
You will see that this allows you to create a HTML page, format it, make it look like you want. You can test it by running it as HTML. When you want to create a PDF from that page simply pass the parameter _convert=something (Does not matter what that something is) and like magic the page is now PDF. Think of that flexibility. You can control if a page is rendered in HTML or PDF by simply calling it with the appropriate parameter.
There is a note on dompdf. It says it support style sheets. SO you might need to play with the -b switch if style sheets or images do not appear. Anyway, here is the new convert PDF code for CCS.
It is not particularly pretty since I just hacked up the original code generated by CCS. But I think you will get the idea.
If you have any questions or need some help, let me know. I hope this helps as I sp[ent a very long time to get this working and I really don't want to see someone else go through that.
Take care and have fun..
CCS Code:
global $main_block;
$server=" -s ".$_SERVER["SCRIPT_FILENAME"]." ";// Fixes a big in running dompdf in PHP CGI mode rather than CLI mode
$Command = "php -f dompdf.php -- "; // Relative path for the -f switch for the dompdf script
// $Options = " -f {ResultFile} {InputFile} ";
$TempFolder = "./tmp/"; //Relative to this directory CCS Script is executing in
$ResultFolder = "./tmp/";//Relative to directory dompdf is running in
if (CCSubStr($TempFolder, -1) != DIRECTORY_SEPARATOR && CCSubStr($TempFolder, -1) != "/" && CCSubStr($TempFolder, -1) != "\\") $TempFolder .= DIRECTORY_SEPARATOR;
$TempFileName = "pdf" . session_id() . mt_rand(100000, 999999);
$ResultFileName = $TempFolder.$TempFileName. ".pdf"; //Where CSS will look for created PDF by dompdf
$InFileName=$ResultFolder.$TempFileName.".html"; //Where dompdf will find the html to convert
$OutFileName=$ResultFolder.$TempFileName.".pdf"; //Where dompdf will put the pdf output
$TempFileName=$TempFolder.$TempFileName.".html"; //Where CSS will put the HTML for dompdf to convert
// echo $TempFileName." ".$ResultFileName;die();
$WindowsQuote = DIRECTORY_SEPARATOR == "\\" ? '"' : "";
if ($TempFileName && $TmpFH = @fopen($TempFileName, "wb")) {
fwrite($TmpFH, $main_block);
fclose($TmpFH);
// echo $TempFileName." ".$ResultFileName;die();
// $Options = str_replace("{InputFile}", escapeshellarg($TempFileName), $Options);
// $Options = str_replace("{Root}", escapeshellarg(getcwd()), $Options);
if ($go==1) {
// $Options = str_replace("{ResultFile}", escapeshellarg($ResultFileName), $Options);
// $Command=$Command.chr(32).chr(32).$ResultFileName.chr(32).chr(32).$TempFileName;
$Command=$Command." -f ".$OutFileName." -b ./ ".$server." ".$InFileName;
exec($Command);
// echo "<br>".$PDFfilename." ".$ResultFileName."</br>".$Command;die();
if ($fh = @fopen($ResultFileName , "rb")) {
$now = time();
$ServerDate = gmdate("D, d M Y H:i:s", $now) . " GMT";
$ExpireDate = gmdate("D, d M Y H:i:s", $now + 60) . " GMT";
Header("Cache-control: ");
Header("Pragma: ");
Header("Date: " . $ServerDate);
Header("Expires: " . $ExpireDate);
Header("Content-Type: application/pdf");
while($str = fread($fh, 1024)) echo $str;
fclose($fh);
unlink($ResultFileName);
$Page_BeforeOutput = false;
}
} elseif ($fh = popen($WindowsQuote . $Command . " " . $Options . $WindowsQuote, "rb")) {
$firstPass = true;
while($str = fread($fh, 1024)) {
if ($firstPass) {
$now = time();
$ServerDate = gmdate("D, d M Y H:i:s", $now) . " GMT";
$ExpireDate = gmdate("D, d M Y H:i:s", $now + 60) . " GMT";
Header("Cache-control: ");
Header("Pragma: ");
Header("Date: " . $ServerDate);
Header("Expires: " . $ExpireDate);
Header("Content-Type: application/pdf");
$firstPass = false;
}
echo $str;
}
pclose($fh);
$Page_BeforeOutput = false;
}
unlink($TempFileName);
}
}
That's It.....
_________________
John Real - More CodeCharge Studio Support at - http://CCSElite.com |
 |
 |
ckroon
Posts: 869
|
| Posted: 02/04/2008, 2:40 PM |
|
Thanks so much.
I have uploaded everything to the server and will now experiment :)
Thanks for all your work.
I wonder if Peterr is thinking about adding this to 4.2...?
:) we can only hope.
_________________
Walter Kempees...you are dearly missed. |
 |
 |
Zye
Posts: 56
|
| Posted: 02/04/2008, 8:27 PM |
|
Many thanks for sharing your hard work John 
I have spent hours tweaking lines in fpdf.
This sounds way cool.
You da man!!!! 
Hope I can get my head around it. 
Cheers ...
|
 |
 |
jjrjr1
Posts: 942
|
| Posted: 02/05/2008, 6:06 AM |
|
Hope it goes easy and workds like you want.
The documentation says thet dompdf does ont work well with nested tables. I have not tried that so be aware when designing your html page.
Also, if you want page breaks in the output pds, use the same style function you would use for page breakes in html.
eg:
<table style="PAGE-BREAK-BEFORE: always; WIDTH: 100%" align="center">
"stuff"
</table>
Let me know if you need any more info or help.
_________________
John Real - More CodeCharge Studio Support at - http://CCSElite.com |
 |
 |
kawuat
Posts: 13
|
| Posted: 02/06/2008, 1:50 PM |
|
Hi,
I've tried implementing the code below but have had issues, it simply outputs to the html view.
can you provide assistance,
I copied and renamed the case as you said, and aslo included the -s case statement
case "cgi":
$opts = getoptions();
if ( isset($opts["h"]) || (!isset($opts["filename"]) && !isset($opts["l"])) ) {
dompdf_usage();
exit;
}
if ( isset($opts["l"]) ) {
echo "\nUnderstood paper sizes:\n";
foreach (array_keys(CPDF_Adapter::$PAPER_SIZES) as $size)
echo " " . mb_strtoupper($size) . "\n";
exit;
}
$file = $opts["filename"];
if ( isset($opts["p"]) )
$paper = $opts["p"];
else
$paper = DOMPDF_DEFAULT_PAPER_SIZE;
if ( isset($opts["o"]) )
$orientation = $opts["o"];
else
$orientation = "portrait";
if ( isset($opts["b"]) )
$base_path = $opts["b"];
if ( isset($opts["f"]) )
$outfile = $opts["f"];
else {
if ( $file == "-" )
$outfile = "dompdf_out.pdf";
else
$outfile = str_ireplace(array(".html", ".htm", ".php"), "", $file) . ".pdf";
}
if ( isset($opts["v"]) )
$_dompdf_show_warnings = true;
if ( isset($opts["d"]) ) {
$_dompdf_show_warnings = true;
$_dompdf_debug = true;
}
$save_file = true;
break;
did the added you code to the pages before output - convert to pdf action
//Page_BeforeOutput @1-26910F65
function Page_BeforeOutput(& $sender)
{
$Page_BeforeOutput = true;
$Component = & $sender;
$Container = & CCGetParentContainer($sender);
global $print_appoinments; //Compatibility
//End Page_BeforeOutput
//Convert Page to PDF @138-990CE052
if (CCGetFromGet("_convert")) {
global $main_block;
$server=" -s ".$_SERVER["SCRIPT_FILENAME"]." ";// Fixes a big in running dompdf in PHP CGI mode rather than CLI mode
$Command = "php -f dompdf.php -- "; // Relative path for the -f switch for the dompdf script
// $Options = " -f {ResultFile} {InputFile} ";
$TempFolder = "./tmp/"; //Relative to this directory CCS Script is executing in
$ResultFolder = "./tmp/";//Relative to directory dompdf is running in
if (CCSubStr($TempFolder, -1) != DIRECTORY_SEPARATOR && CCSubStr($TempFolder, -1) != "/" && CCSubStr($TempFolder, -1) != "\\") $TempFolder .= DIRECTORY_SEPARATOR;
$TempFileName = "pdf" . session_id() . mt_rand(100000, 999999);
$ResultFileName = $TempFolder.$TempFileName. ".pdf"; //Where CSS will look for created PDF by dompdf
$InFileName=$ResultFolder.$TempFileName.".html"; //Where dompdf will find the html to convert
$OutFileName=$ResultFolder.$TempFileName.".pdf"; //Where dompdf will put the pdf output
$TempFileName=$TempFolder.$TempFileName.".html"; //Where CSS will put the HTML for dompdf to convert
I get no errors but neither do i get a pdf
|
 |
 |
jjrjr1
Posts: 942
|
| Posted: 02/07/2008, 5:21 AM |
|
Hi
This usually happens when dompdf is unable to read or create the file.
You need to be sure the tempprary directory paths are correct relative to where CSS scripts and dompdf is running. If for any reason dompdf fails the p[age will render as html.
Look at the CSS code and you will see the conditionals that test for files being created.
Rhe easiest way to debug is to change the exec statement to a passthu and have a die() statement at a judicious spot. The you can look at any errors dompdf generates as well as any variables you might want to verify. If you do the die() right after the shell command you can also look at the server and see if the files are being created as expected.
Also, the temporary directory needs to be set wkth the chmod statement to have write privledges. smething like chmod 777 or 666.
Let me know if that helps.
_________________
John Real - More CodeCharge Studio Support at - http://CCSElite.com |
 |
 |
jjrjr1
Posts: 942
|
| Posted: 02/16/2008, 3:54 PM |
|
Hi
If anyone has had a problem getting ffmpeg to run from the exec command in CCS, try this
In the exec statement I found some servers require the full path name to execute ffmpeg.
that path should be /usr/local/bin/ffmpeg on most servers.
Hope that helps if you are having problems
_________________
John Real - More CodeCharge Studio Support at - http://CCSElite.com |
 |
 |
jjrjr1
Posts: 942
|
| Posted: 02/16/2008, 4:22 PM |
|
The above post was meant for the ffmpeg post.
Sorry..
_________________
John Real - More CodeCharge Studio Support at - http://CCSElite.com |
 |
 |
studio_two
Posts: 27
|
| Posted: 03/31/2008, 2:26 AM |
|
I was just reading through this thread and spotted this on the dompdf web site....
Security vulnerability in dompdf: http://www.digitaljunkies.ca/dompdf/
BTW, John - thanks for all the code
|
 |
 |
jjrjr1
Posts: 942
|
| Posted: 04/01/2008, 9:03 AM |
|
Thanks for the heads up.
Will make the adjustments.
Take Care.
_________________
John Real - More CodeCharge Studio Support at - http://CCSElite.com |
 |
 |
SimonB
Posts: 3
|
| Posted: 05/01/2008, 5:37 PM |
|
Just a quick note about using the existing Convert to PDF Action. It is possible to use this without replacing it with John's code above. The key is to place the call to php in the command field then in the Options field enter: -f dompdf.php -- -f {ResultFile} -d {Root} {InputFile}.
Where ever possible I like to try and keep generated code unmodified 
One thing I have not got working is images in the resulting PDF file. All I get is a X and I am unsure if this is a problem with how I am implementing it or if it is a problem with dompdf. From the range of comments on the dompdf forum it looks like images are a common problem. Does anyone have this working?
|
 |
 |
jjrjr1
Posts: 942
|
| Posted: 05/02/2008, 9:45 AM |
|
Hi Simonb
I agree about changing generated code when ever possible,
If you want to do that don't create a comvert to pdf event. Simply place this code in a custom code action in the before output event. All the before output stuff is run at one time and if you put it in custom code you won't be changing generated code.
The problem with the generated code is it does not have all the stuff you need to do for calling dompdf.
For example you so need to set the -s value since dompdf needs that parameter to function properly and not all hosts supply that value when a program is running in CGI mode as DOMPDF is in this case. (Did you make the mods to dompdf?)
To get the images to show properly you need to set the -b path for dompdf relative to where dompdf is running. The same is the case with style sheets you might be using.
Let me know if that helps.
JOhn
_________________
John Real - More CodeCharge Studio Support at - http://CCSElite.com |
 |
 |
jjrjr1
Posts: 942
|
| Posted: 05/02/2008, 9:58 AM |
|
Another Note.
Be sure your directories you are writing to have appropriate permissions. Also lib/fonts in the dompdf code needs to have write permissions as well. You can verify this by running the passthru command instead of exec then do a die() statement to see if DOMPDF is generating any errors.
This might also show your pathing problems for images.
John
_________________
John Real - More CodeCharge Studio Support at - http://CCSElite.com |
 |
 |
|