|
|
1 - Whats all this crap I see, my page is broken ? 2 - What page sizes can I use ? 3 - What is this Call to undefined function: imagecreatefromjpeg() error about ? 4 - Does this work with accented characters ? 5 - How can I write the PDF file to a static file on the webserver? 6 - How do I do Hello World? 7 - Can I put the fonts directory somewhere else ? 8 - Can I see the code behind the online demo ? 9 - What values can I give for the setPreferences method labels? 10 - my accented characters do not work with full justification? 11 - I just get a blank page? 12 - So what are the page sizes? 13 - why am I getting this array_search() function missing? 14 - I get an error which says bad value for BBOX in font! 15 - How should I do debugging ?! 16 - Is there a code example for writing the file to disk and redirecting? 17 - How do I get the Euro character to show in my text? 18 - How do I get the symbolic fonts (eg zapfdingbats) working? 19 - How do I make a table from my database information?
| |
 |
 | 1 - Whats all this crap I see, my page is broken ?
|  |
 |  | If when you try to view your pdf document, you just get a whole lot of "junk", that looks like this:
%PDF-1.3%âãÏÓ10obj<</Type/Catalog/Outlines20R/Pages30R>>endobj20obj
<</Type/Outlines/Count0>>endobj30obj<</Type/Pages/Kids[60R]...
(and a whole lot more)
This is most likely due to a problem with IE not refreshing the mime type of a page. What can happen is if you have had an error in the creation of the page in the past, then you fix the error, but just keep refreshing the page, IE does not re-register the mime-type of the page as a pdf, but keeps it being a text page (like it was when there was the error).
The solution is simple. Shut the browser (copying the URL before you do it makes life easier), then open a new one, and paste in the URL - it then picks up that it is a pdf page
(you don't actually have to start a new browser, simply opening the current one onto a new page is sufficient, however when you put in the new page, you may have to then refresh to get it to come up, the first time will get the broken page from the cache and display it immediatly, a refresh then will open up the pdf document)
|  |
 |
 | 2 - What page sizes can I use ?
|  |
 |  | In version 009 the number of page sizes has been expanded dramatically - these were all set up for me by Nicola Asuni (thanks)
You can choose from one of "4A0, 2A0, A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, B0, B1, B2, B3, B4, B5, B6, B7, B8, B9, B10, C0, C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, RA0, RA1, RA2, RA3, RA4, SRA0, SRA1, SRA2, SRA3, SRA4, LETTER, LEGAL, EXECUTIVE, FOLIO", and it is not case sensitive.
As well as that, if you specify the size as an array of two elements, then that is used as the page width and heigt in centimenters, or an array of 4 elements which will then be used to specify the lower left and upper right corners in pdf units (approx points).
NOTE, that this only applies to the ezPdf class.
|  |
 |
 | 3 - What is this Call to undefined function: imagecreatefromjpeg() error about ?
|  |
 |  |
Well, sorry, but I forgot to mention that to use the demo you will have to
have GD installed with JPEG support, if you don't then the demo will run
fine (but without the image) if you comment out the lines 66->69 of
readme.php. I am going to add the facility to include an jpeg image straight
from a file without the specific jpeg support once I ensure that it works
the way that I think it should.
update - this functionality has been added, see the documentation for the addJpegFromFile command, there is also support for PNG files (addPngFromFile)
|  |
 |
 | 4 - Does this work with accented characters ?
|  |
 |  |
Being a slightly ignorant english speaker I completely forgot about
accented characters, it is near the top of the list of things to fix up.
|  |  |
update 24 Nov 2001 - I have made a small change to the class which should make the accented characters work better, if you have downloaded already, then just updating the class file will do. This has only been tested upder windows and works for all the characters I tried, please tell me if you find some which you think it should do, but it doesn't.
|  |  |
update Nov 2002 - There has been a lot of work done on accented charcter support since the previous messages were written, it should now be fairly useful, see the FAQ entry below on problems with accented characters, and full justification (though this applies to any character which is outside of the normal windows set, not just accented ones).
|  |
 |
 | 5 - How can I write the PDF file to a static file on the webserver?
|  |
 |  | Simplicity itself, just use the same functions that are used to extract the raw code for debugging:
$pdfcode = $pdf->output();
$fp=fopen('','wb');
fwrite($fp,$pdfcode);
fclose($fp);
Note that if you are using the ezPdf class, then you will need to modify this slightly as shown below to get it to work (the most obvios symptom of forgetting this will be missing page numbers).
$pdfcode = $pdf->ezOutput();
$fp=fopen('','wb');
fwrite($fp,$pdfcode);
fclose($fp);
|  |
 |
 | 6 - How do I do Hello World?
|  |
 |  | It would be horrible to break with tradition, so here it is:
include 'class.pdf.php';
// make a new pdf object
$pdf = new Cpdf();
// select the font
$pdf->selectFont('./fonts/Helvetica');
$pdf->addText(30,400,30,'Hello World');
$pdf->stream();
|  |
 |
 | 7 - Can I put the fonts directory somewhere else ?
|  |
 |  | Yes, the selectFont function will take an absolute path so you can specify where you would like the
fonts to live, of course the web server will have to have at least read access to them, and if for some
reason the 'php_*' copies of the font files are missing (the serialised ones), then the server will need write access
on the first use of each font to write the files.
for example:
$pdf->selectFont('/home/fonts/Helvetica');
|  |
 |
 | 8 - Can I see the code behind the online demo ?
|  |
 |  | Sure, here it is:
if (!isset($demotext) || strlen($demotext)==0){
$demotext='demo text';
}
include 'class.pdf.php';
$pdf = new Cpdf(array(0,0,598,842));
$pdf->selectFont('./fonts/Helvetica');
// do some funky stuff in the background, in a nice light blue, which is bound to clash with something
// and some red for the hell of it
$x=578;
$r1=25;
for ($xw=40;$xw>0;$xw-=3){
$tone=1.0-$xw/40*0.2;
$pdf->setLineStyle($xw);
$pdf->setStrokeColor($tone,1,$tone);
$pdf->ellipse(50,750,$r1);
$r1=$r1+$xw;
}
for ($xw=40;$xw>0;$xw-=3){
$tone=1.0-$xw/40*0.2;
$pdf->setStrokeColor($tone,$tone,1);
$pdf->setLineStyle($xw);
$pdf->line($x,0,$x,842);
$x=$x-$xw-2;
}
$pdf->setStrokeColor(0,0,0);
$pdf->setLineStyle(1);
$pdf->rectangle(20,20,558,802);
$y=800;
for ($size=50;$size>5;$size=$size-5){
$height = $pdf->getFontHeight($size);
$y=$y-$height;
$pdf->addText(30,$y,$size,$demotext);
}
for ($angle=0;$angle<360;$angle=$angle+20){
$r=rand(0,100)/100;
$g=rand(0,100)/100;
$b=rand(0,100)/100;
$pdf->setColor($r,$g,$b);
$pdf->addText(300+cos(deg2rad($angle))*40,300-sin(deg2rad($angle))*40,20,$demotext,$angle);
}
$pdf->stream();
|  |
 |
 | 9 - What values can I give for the setPreferences method labels?
|  |
 |  |
from the PDF specification:
HideToolbar boolean (Optional) A flag specifying whether to hide the viewer application’s tool
bars when the document is active. Default value: false.
HideMenubar boolean (Optional) A flag specifying whether to hide the viewer application’s
menu bar when the document is active. Default value: false.
HideWindowUI boolean (Optional) A flag specifying whether to hide user interface elements in
the document’s window (such as scroll bars and navigation controls),
leaving only the document’s contents displayed. Default value: false.
FitWindow boolean (Optional) A flag specifying whether to resize the document’s window to
fit the size of the first displayed page. Default value: false.
CenterWindow boolean (Optional) A flag specifying whether to position the document’s window
in the center of the screen. Default value: false.
NonFullScreenPageMode name (Optional) The document’s page mode, specifying how to display the
document on exiting full-screen mode:
UseNone Neither document outline nor thumbnail images visible
UseOutlines Document outline visible
UseThumbs Thumbnail images visible
This entry is meaningful only if the value of the PageMode entry in the
catalog dictionary is FullScreen;
it is ignored otherwise. Default value: UseNone.
Direction name (Optional; PDF 1.3) The predominant reading order for text:
L2R Left to right
R2L Right to left (including vertical writing systems
such as Chinese, Japanese, and Korean)
This entry has no direct effect on the document’s contents or page numbering,
but can be used to determine the relative positioning of pages
when displayed side by side or printed n-up. Default value: L2R.
note that boolean values are represented by the values 'true' and 'false'
also note that I have not done much testing on changing these values and am not sure how responsive
the various viewers and browsers are to them (and setting the direction would be fairly meaningless as none of
these character sets are avaliable yet.
|  |
 |
 | 10 - my accented characters do not work with full justification?
|  |
 |  |
yes, this is a bit of a problem, however the latest version does give you the capability to fix the problem. I beleive that it stems from some discrepancy between the character numbering in the AFM file, and that used within the various operating systems, which results in the character at the position is one which has small (or zero) width. However the latest version gives you the capability to 're-assign' character numbers to named adobe font characters (which gives you potential access to the full set), the function is an option of the font selection command and it should be documented in the pdf file.
Here is the diff array for the typical german characters, which was sent to me by another guy that helped me sort this out.
$diff=array(196=>'Adieresis',228=>'adieresis',
214=>'Odieresis',246=>'odieresis',
220=>'Udieresis',252=>'udieresis',
223=>'germandbls');
// and the first time that you call selectFont for each font, use
$pdf->selectFont('<fontname>',array('encoding'=>'WinAnsiEncoding','differences'=>$diff));
|  |
 |
 | 11 - I just get a blank page?
|  |
 |  | There is a bug in IE, it seems to occur in a few versions, including 5, 5.5, 6. Under some circustances it will cause just a blank page to be displayed when a pdf is streamed to the browser. The best known solution to this problem is to write the pdf document to a file, then re-direct the user to the file, though not with a location: header, that also causes an error, you re-direct with javascript.
|  |  | Also this information from Steve Weisz
Regarding PDF class on secure servers & link technique vs. post method
Using the "post" method, like when using a form post, to create a pdf on a secure server will cause the page to be insecure. In IE 5.5, nothing will display (I believe some of the blank page feedback you have received). IE 6 asks if you want to display the insecure page and upon acceptance will show it to you. The easiest solution to maintain the integrity of the secure page is to create a link (with your string of variables) to the pdf creating document.
|  |
 |
 | 12 - So what are the page sizes?
|  |
 |  | The coordinates that are used for the pages are:
letter = 612 x 792
a4 = 595.4 x 842 ( note that this was 598x842 up till version 007, will be the more accurate figure in 008)
|  |
 |
 | 13 - why am I getting this array_search() function missing?
|  |
 |  | Sorry, the early versions of PHP4 did not have this function, so as an alternative to upgrading to a later version of PHP you can put this function into your code:
function array_search($needle, $haystack) {
$match = false;
foreach ($haystack as $key => $value) {
if ($key == $needle) {
$match = $key;
}
}
return $match;
}
|  |
 |
 | 14 - I get an error which says bad value for BBOX in font!
|  |
 |  |
This error almost always occurs because there is some problem reading or accessing the font files, ensure that:
- the files are all in the right place (usually in a fonts folder below the class location)
- the web server has access to the files (has at least read rights to the files)
- the web server may also need write access to the folder to make the php_ files
- ensure that the case of the files is the same as the originals, this is especially important for the 14 stadard fonts.
- if adding your own ttf or pfb fonts, then the suffix needs to be lower case, and the fontname for the .pfb file (or .ttf file) must be identical to that in the .afm file.
|  |
 |
 | 15 - How should I do debugging ?!
|  |
 |  |
here is the code that I include at the bottom of all my test scripts:
$pdf->ezText("\n\n".$pdf->messages,10,array('justification'=>'left'));
if (isset($d) && $d){
$pdfcode = $pdf->output(1);
$end_time = getmicrotime();
$pdfcode = str_replace("\n","\n<br>",htmlspecialchars($pdfcode));
echo '<html>';
echo trim($pdfcode);
echo '</body>';
} else {
$pdf->stream();
}
Then I generate the pdf with &d=1 on the URL until there are no warnings, then set it to 0 to see if the pdf works.
As when debug mode is on (d=1) the pdf code is shown uncompressed, you can still pick out the messages in the code - they appear plainly on the screen when debug is off (d=0).
|  |
 |
 | 16 - Is there a code example for writing the file to disk and redirecting?
|  |
 |  |
Yes, here is some code that I use for doing that - I have never had this code bring up a nasty blank IE page:
$pdfcode = $pdf->output();
$dir = './pdf_files';
//save the file
if (!file_exists($dir)){
mkdir ($dir,0777);
}
$fname = tempnam($dir.'/','PDF_').'.pdf';
$fp = fopen($fname,'w');
fwrite($fp,$pdfcode);
fclose($fp);
echo '<html>
<head>
<SCRIPT LANGUAGE="JavaScript"><!--
function go_now () { window.location.href = "'.$fname.'"; }
//--></SCRIPT>
</head>
<body onLoad="go_now()"; >
<a href="'.$fname.'">click here</a> if you are not re-directed.
</body>
</html>
';
// also have a look through the directory, and remove the files that are older than a week
if ($d = @opendir($dir)) {
while (($file = readdir($d)) !== false) {
if (substr($file,0,4)=="PDF_"){
// then check to see if this one is too old
$ftime = filemtime($dir.'/'.$file);
if (time()-$ftime > 3600*24){
unlink($dir.'/'.$file);
}
}
}
closedir($d);
}
Note that this code will also scan the folder and will remove any files which are more than a day old.
|  |
 |
 | 17 - How do I get the Euro character to show in my text?
|  |
 |  | The Euro character seems to be outside the normal character set, so you need to replace one of your 0->255 characters with it, using the options of the selectFont command (noting that these only work the first time that the function is called for each font).
Here is an example which replaces the "!" character:
<?php
include ('class.ezpdf.php');
$pdf =& new Cezpdf('a4');
$euro_diff = array(33=>'Euro');
$pdf->selectFont('./fonts/Helvetica.afm',
array('encoding'=>'WinAnsiEncoding','differences'=>$euro_diff));
$pdf->ezText('**!!!!!!**',100,array('justification'=>'full'));
$pdf->ezText($pdf->messages,12);
if (isset($d) && $d){
$pdfcode = $pdf->ezOutput(1);
$pdfcode = str_replace("\n","\n<br>",htmlspecialchars($pdfcode));
echo '<html><body>';
echo trim($pdfcode);
echo '</body></html>';
} else {
$pdf->ezStream();
}
?>
Note that this is most likely not the actual character that you wish to replace, but here is some information which was sent to me by "Balu":
Just for the reference (FAQ? :-), I've found more references about using
the Euro in ASCII-Tables:
http://www.dragoman.org/euro/
replaces the "^"-char (0x5E) by the Euro-Symbol (U+20AC) and calls the
new Table "Euro-ASCII"
http://lists.w3.org/Archives/Public/www-international/1997OctDec/att-0077/01-euro.html
replaces the "|" (0x7C) by the Euro
http://www.alphaquark.com/Traduction/En_Euro.htm
replaces the ascii-char at 128 (0x80)
My Linux-console (vim + xxd) uses 0xa4...
|  |
 |
 | 18 - How do I get the symbolic fonts (eg zapfdingbats) working?
|  |
 |  | It seems that these need to have the encoding set to "none" in order to work, this should be done on the first selectFont command for the font, and will look something like:
$pdf->selectFont('ZapfDingbats.afm','none');
or:
$pdf->selectFont('ZapfDingbats.afm',array('encoding'=>'none'));
|  |
 |
 | 19 - How do I make a table from my database information?
|  |
 |  | This is relatively simple, you must populate an array from the database, and then use that to make the pdf table. As there seeems to be a lot of trouble with this, here is an example:
<?php
// test the table functions
error_reporting(E_ALL);
include('class.ezpdf.php');
$pdf =& new Cezpdf();
$pdf->selectFont('./fonts/Helvetica');
//--------------------------------------------------
// you will have to change these to your settings
$host = 'localhost';
$user = 'root';
$password = 'secretpassword';
$database = 'mydatabasename';
$query = 'select * from user';
//--------------------------------------------------
// open the connection to the db server
$link = mysql_connect($host,$user,$password);
// change to the right database
mysql_select_db($database);
// initialize the array
$data = array();
// do the SQL query
$result = mysql_query($query);
// step through the result set, populating the array, note that this could also have been written:
// while($data[] = mysql_fetch_assoc($result)) {}
while($data[] = mysql_fetch_array($result, MYSQL_ASSOC)) {}
// make the table
$pdf->ezTable($data);
// do the output, this is my standard testing output code, adding ?d=1
// to the url puts the pdf code to the screen in raw form, good for checking
// for parse errors before you actually try to generate the pdf file.
if (isset($d) && $d){
$pdfcode = $pdf->output(1);
$pdfcode = str_replace("\n","\n<br>",htmlspecialchars($pdfcode));
echo '<html><body>';
echo trim($pdfcode);
echo '</body></html>';
} else {
$pdf->stream();
}
?>
|  |  | |