Create PDF file with PHP

Create PDF file with PHP Posted in PHP | 8 Comments

PDFs have become widespread enough among businesses and institutions to make automatically and dynamically producing them in large quantity is a valuable skill to learn. Portable Document Files from Adobe are easily produced in PHP, making it an even better glue language than before. Since anyone can install PHP, they can use the simple PHP 5 class of PDFlib that’s provided to make a as many PDFs in whatever format you need.

The irony is, of course, PDFlib is now commercial and offers a PDFlib Lite. Either way, the PDFlib library has an unwieldy learning curve, and instead, I recommend FPDF for your general PDF usages. This is a freeware PDF library written completely in PHP, so no PECL or PEAR installation is required, which does slow it down. So, a large business shouldn’t use this to process hundreds of PDFs, but for smaller jobs it works.

Making a basic PDF document on the fly

I’ll show you how to make a basic PDF document with text blocks of different alignments, as well as double-spacing, headers and footers and displaying an image.

Let’s jump immediately into an example showing all of this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
require('classes/fpdf/fpdf.php');
class PDF extends FPDF {
function Header() {
    $this->SetFont('Times','',12);
    $this->SetY(0.25);
    $this->Cell(0, .25, "John Doe ".$this->PageNo(), 'T', 2, "R");
    //reset Y
    $this->SetY(1);
}
function Footer() {
//This is the footer; it's repeated on each page.
//enter filename: phpjabber logo, x position: (page width/2)-half the picture size,
//y position: rough estimate, width, height, filetype, link: click it!
    $this->Image("logo.jpg", (8.5/2)-1.5, 9.8, 3, 1, "JPG", "http://www.phpjabbers.com");
}
}
//class instantiation
$pdf=new PDF("P","in","Letter");
$pdf->SetMargins(1,1,1);
$pdf->AddPage();
$pdf->SetFont('Times','',12);
$lipsum1="Lorem ipsum dolor sit amet, nam aliquam dolore est, est in eget.";
 
$lipsum2="Nibh lectus, pede fusce ullamcorper vel porttitor.";
 
$lipsum3 ="Duis maecenas et curabitur, felis dolor.";
 
$pdf->SetFillColor(240, 100, 100);
$pdf->SetFont('Times','BU',12);
 
//Cell(float w[,float h[,string txt[,mixed border[,
//int ln[,string align[,boolean fill[,mixed link]]]]]]])
$pdf->Cell(0, .25, "lipsum", 1, 2, "C", 1);
 
$pdf->SetFont('Times','',12);
//MultiCell(float w, float h, string txt [, mixed border [, string align [, boolean fill]]])
$pdf->MultiCell(0, 0.5, $lipsum1, 'LR', "L");
$pdf->MultiCell(0, 0.25, $lipsum2, 1, "R");
$pdf->MultiCell(0, 0.15, $lipsum3, 'B', "J");
$pdf->AddPage();
$pdf->Write(0.5, $lipsum1.$lipsum2.$lipsum3);
 
$pdf->Output();
?>

I create a PDF in Portrait mode with a Letter size and measured in inches. Then, I set the margins to 1 inch, add our first page, and then set the Font to Times, Size 12. Then, my 3 dummy texts, and after that I set the fill color that will be the space behind my title’s text with a bold/underlined modifier. After that, I print the title, then reset the font and specify some different types of MultiCells. The page is made up of cells on an x,y plane in FPDF, which makes it pretty convenient to work with for basic PDF editing.

The headers and footers are functions automatically called in the parent class, but they do nothing by default. That means we have to redefine them to our needs. In this case, a name and page number as the header and a logo with a link as the footer. These are repeated on each page.

Note that in order to place the text in the proper location, I had to use the SetX and SetY functions. Don’t forget to reset them to their normal flow when you’re done in the header or footer, though!

Cells are individual units on the page. It is convenient to define a line with a single cell, but at the same time this can be achieved much more quickly by use of the MultiCell function which will automatically create a new cell as text is passed through. Write is just streamed text.

One thing to absolutely keep in mind is that you can’t output any text to the browser when trying to send this PDF to the browser, if you’re making it on the fly. If you’re making it for storage or later use, then there’s no problem outputting text to the user as well. This is accomplished by setting the parameters to Output appropriately.

The spacing is done with the line height. You have to do some math to figure out what you want. The line height is the entire height of the line, which means the lines are up against each other. That means whatever you set for the line height is spacing between the letters, as well!

With these skills in mind, you should be able to do whatever you want to a PDF. For even more functionality, there is a Scripts section, but of course there is always the PDFlib library which provides all kinds of specialization. FPDF is a highly documented library though, and it’s really simple to use. Good luck!

Advertisements
By Rz Rasel Posted in Php

6 comments on “Create PDF file with PHP

  1. You really make it seem so easy together with your presentation however I find this topic to be really one thing that I believe I might by no means understand. It sort of feels too complex and very huge for me. I’m having a look ahead for your next put up, I will attempt to get the dangle of it!

  2. Hello Web Admin, I noticed that your On-Page SEO is is missing a few factors, for one you do not use all three H tags in your post, also I notice that you are not using bold or italics properly in your SEO optimization. On-Page SEO means more now than ever since the new Google update: Panda. No longer are backlinks and simply pinging or sending out a RSS feed the key to getting Google PageRank or Alexa Rankings, You now NEED On-Page SEO. So what is good On-Page SEO?First your keyword must appear in the title.Then it must appear in the URL.You have to optimize your keyword and make sure that it has a nice keyword density of 3-5% in your article with relevant LSI (Latent Semantic Indexing). Then you should spread all H1,H2,H3 tags in your article.Your Keyword should appear in your first paragraph and in the last sentence of the page. You should have relevant usage of Bold and italics of your keyword.There should be one internal link to a page on your blog and you should have one image with an alt tag that has your keyword….wait there’s even more Now what if i told you there was a simple WordPress plugin that does all the On-Page SEO, and automatically for you? That’s right AUTOMATICALLY, just watch this 4minute video for more information at. WordPress Seo Plugin

  3. Hello. I noticed your website title, “Create PDF file with PHP | arzrasel” doesn’t really reflect the content of your web site. When creating your website title, do you believe it’s most beneficial to write it for Search engine marketing or for your visitors? This is something I’ve been battling with mainly because I want great search rankings but at the same time I want the best quality for my site visitors.

  4. Howdy! Do you know if they make any plugins to safeguard against hackers? I’m kinda paranoid about losing everything I’ve worked hard on. Any suggestions?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s