Adding text/images to an existing PDF

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

User avatar
JayBird
Admin
Posts: 4522
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Adding text/images to an existing PDF

Post by JayBird »

First of all, download both the following packages

http://www.fpdf.org/

http://fpdi.setasign.de/index.php?p=home


Extract both packages into the same folder.

Put the PDF you want to add text to in the same folder and call it test1.pdf

Create a new script in the same folder called demo.php and add the following code.

Code: Select all

define('FPDF_FONTPATH','font/');
require('fpdi.php');

class PDF extends fpdi
{
//Page header
function Header()
{
    //Logo
    //$this->Image('logo_pb.png',10,8,33);
    //Arial bold 15
    //$this->SetFont('Arial','B',15);
    //Move to the right
    //$this->Cell(80);
    //Title
    //$this->Cell(30,10,'Title',1,0,'C');
    //Line break
    //$this->Ln(20);
}

//Page footer
function Footer()
{
    //Position at 1.5 cm from bottom
    $this->SetY(-15);
    //Arial italic 8
    $this->SetFont('Arial','I',8);
    //Page number
    $this->Cell(0,10,'This document was downloaded at '.date("G:i T", time()).' on '.date("l F jS Y", time()).'. This document is only valid for 24 hours from this date.',0,0,'C');
}
}

$pdf= new PDF();

$pagecount = $pdf->setSourceFile("test1.pdf");


for ($i=1; $i <= $pagecount; $i++) {

	$tplidx = $pdf->ImportPage(1);

	$pdf->addPage();
	$pdf->useTemplate($tplidx,0,0,0);
}


$pdf->Output("newpdf.pdf","I");
Easy peasy...obviously there is loads more you can do with this, but this should get ya started.

Uncomment the header function and change the image name to one that exists to add images!

Mark
Last edited by JayBird on Tue Dec 20, 2005 5:13 am, edited 2 times in total.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Thats mightily cool. I might be doing client reports in PDF format after I've had a play..

Maybe..
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

Plz Tell me how I can insert SVG graphics into PDF using any of the PDF lib available out there.
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

Plz reply somebody. :( Hey Feyd , can't you throw some light on this matter.
plz. i want to able to insert svg into pdf. anyhow...

thanx in advance...
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Have you tried searching? Probably not..
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

in all probability, you will need to convert the SVG to a supported image format, PNG or JPEG.
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

Post by fresh »

you could probably spoof the server into believing the SVG is a PNG and if all goes well the server will gladly display the picture or put it into your PDF, whatever you want. I know this is possible with files such as Batch, you could spoof the server to make it think it is an exe and the user downloading would download it as an exe and of course the batch file wont run, but the point is that spoofing the server is possible. You do it with content-type headers.

gdlk

p.s. let us know if this solves your problem. :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

what does content-type have to do with importing an unsupported image type into a PDF ?
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

Post by fresh »

hmm.. I suppose I read it wrong but I assumed he would mov the SVG file bits into a container in his script and then tell the browser that it is either jpeg or png bits and thus would pass the server check for png or jpeg (depending on the gped version his server has) and would then pipe those bits into his PDF. It was only a suggestion.. perhaps a bad one? :)

regards
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

lol
but I assumed he would mov the SVG file bits into a container in his script and then tell the browser that it is either jpeg or png bits and thus would pass the server check for png or jpeg (depending on the gped version his server has) and would then pipe those bits into his PDF.
lol.

How can you say that.. SVG is a XML format dear.
Only thing I have is BATIK - an SVG Graphics Package developed under XML Images Project.
See http://xml.apache.org/batik for more details.
But there are probs (many of 'em).

#1. It written in Java.
#2. On using it in PHP thru PHP/Java Bridge it generates Null Pointer Exception without any reason.
#3. Even the demo script they have in their docs (in Java) gives that error when I compile and run it.
#4. They've included a SVG-Rasterizer in the package which readily converts SVG into JPEG, PNG & TIFF.
From CMD line it runs OK but if I try to exec() it from a PHP script it does nothing.

These all are driving me nuts, nuts and bigger nuts . Plz help if anybody can.

Thanx 4 any help.
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

I am still stuck at the same point. Feyd, please see if you can turn up something on this matter. It would be a great help for me.
Thanx
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

n00b Saibot wrote:Feyd, please see if you can turn up something on this matter.
why? are your googling powers not so good? :P

Since Batik is open sourced, it shouldn't be too difficult to port to PHP entirely.. I'd bet that with the help of onion2k, you could accomplish such a feat. Although I didn't look at the source to the package, so I'm not entirely sure what other packages are needed.. but I'd imagine just an xml parser and graphics rendering stuffs.. both of which are available in php already :)
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

feyd-the good one wrote: why? are your googling powers not so good?
:lol: that isn't so. i've search extensively before posting here. i have a habit of solving my probs myself. its very rare that i turn to others for help.
methinks this is the first time i have posted a prob.
I thought two brains are better than one. so that's why i....

anyway. thanx very much :D
dermoritz
Forum Newbie
Posts: 9
Joined: Fri May 18, 2007 3:13 am

Post by dermoritz »

i tried it an get parse errors:

Parse error: syntax error, unexpected '&' in /var/www/web3/html/moritz/fpdf153/demo.php on line 27

the line:
$this->SetY(-15);


besides this there are many other errors in it - says zend studio. what i make wrong? could it be some html chracters?
ok, sorry for the noobish question it is html.

but now i get a warning and an error:
Warning: Cannot modify header information - headers already sent by (output started at ..../fpdf153/demo.php:2) in .../fpdf153/fpdf.php on line 1022
FPDF error: Some data has already been output to browser, can't send PDF file
Last edited by dermoritz on Fri May 18, 2007 7:29 am, edited 1 time in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

dermoritz wrote:i tried it an get parse errors:

Parse error: syntax error, unexpected '&' in /var/www/web3/html/moritz/fpdf153/demo.php on line 27

the line:
$this->SetY(-15);


besides this there are many other errors in it - says zend studio. what i make wrong? could it be some html chracters?
The forum has broken the code. > should be >
Post Reply