Jump to content

Create a pdf from my website


Adamhumbug
Go to solution Solved by Barand,

Recommended Posts

Hi all,

I wasnt sure of the best place to put this thread so would appreciate the admins advice on that.

I have a web app and i am wanting it to be able to create a downloadable pdf.

I am early in the process but i am assuming that i create the view that i want to pdf in html/css.

Can anyone suggest the best way for me to go about creating this PDF or any third party add ons that i could be using.

@Barand will be happy to know this will be a PDO project, although a simple one to start.

Link to comment
Share on other sites

  • Solution

The most common one, I think, is FPDF

Another one, which is an enhanced version of that one, is TCPDF but I find the documentation a nightmare (the source file itself seems the only place). There are lots of example but it is difficult to know what the arguments for the methods are without a decent reference manual. For example

// Image example with resizing
$pdf->Image('images/image_demo.jpg', 15, 140, 75, 113, 'JPG', 'http://www.tcpdf.org', '', true, 150, '', false, false, 1, false, false, false);

You then have to dig into the source file to find

/**
	 * Puts an image in the page.
	 * The upper-left corner must be given.
	 * The dimensions can be specified in different ways:<ul>
	 * <li>explicit width and height (expressed in user unit)</li>
	 * <li>one explicit dimension, the other being calculated automatically in order to keep the original proportions</li>
	 * <li>no explicit dimension, in which case the image is put at 72 dpi</li></ul>
	 * Supported formats are JPEG and PNG images whitout GD library and all images supported by GD: GD, GD2, GD2PART, GIF, JPEG, PNG, BMP, XBM, XPM;
	 * The format can be specified explicitly or inferred from the file extension.<br />
	 * It is possible to put a link on the image.<br />
	 * Remark: if an image is used several times, only one copy will be embedded in the file.<br />
	 * @param $file (string) Name of the file containing the image or a '@' character followed by the image data string. To link an image without embedding it on the document, set an asterisk character before the URL (i.e.: '*http://www.example.com/image.jpg').
	 * @param $x (float) Abscissa of the upper-left corner (LTR) or upper-right corner (RTL).
	 * @param $y (float) Ordinate of the upper-left corner (LTR) or upper-right corner (RTL).
	 * @param $w (float) Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
	 * @param $h (float) Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
	 * @param $type (string) Image format. Possible values are (case insensitive): JPEG and PNG (whitout GD library) and all images supported by GD: GD, GD2, GD2PART, GIF, JPEG, PNG, BMP, XBM, XPM;. If not specified, the type is inferred from the file extension.
	 * @param $link (mixed) URL or identifier returned by AddLink().
	 * @param $align (string) Indicates the alignment of the pointer next to image insertion relative to image height. The value can be:<ul><li>T: top-right for LTR or top-left for RTL</li><li>M: middle-right for LTR or middle-left for RTL</li><li>B: bottom-right for LTR or bottom-left for RTL</li><li>N: next line</li></ul>
	 * @param $resize (mixed) If true resize (reduce) the image to fit $w and $h (requires GD or ImageMagick library); if false do not resize; if 2 force resize in all cases (upscaling and downscaling).
	 * @param $dpi (int) dot-per-inch resolution used on resize
	 * @param $palign (string) Allows to center or align the image on the current line. Possible values are:<ul><li>L : left align</li><li>C : center</li><li>R : right align</li><li>'' : empty string : left for LTR or right for RTL</li></ul>
	 * @param $ismask (boolean) true if this image is a mask, false otherwise
	 * @param $imgmask (mixed) image object returned by this function or false
	 * @param $border (mixed) Indicates if borders must be drawn around the cell. The value can be a number:<ul><li>0: no border (default)</li><li>1: frame</li></ul> or a string containing some or all of the following characters (in any order):<ul><li>L: left</li><li>T: top</li><li>R: right</li><li>B: bottom</li></ul> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
	 * @param $fitbox (mixed) If not false scale image dimensions proportionally to fit within the ($w, $h) box. $fitbox can be true or a 2 characters string indicating the image alignment inside the box. The first character indicate the horizontal alignment (L = left, C = center, R = right) the second character indicate the vertical algnment (T = top, M = middle, B = bottom).
	 * @param $hidden (boolean) If true do not display the image.
	 * @param $fitonpage (boolean) If true the image is resized to not exceed page dimensions.
	 * @param $alt (boolean) If true the image will be added as alternative and not directly printed (the ID of the image will be returned).
	 * @param $altimgs (array) Array of alternate images IDs. Each alternative image must be an array with two values: an integer representing the image ID (the value returned by the Image method) and a boolean value to indicate if the image is the default for printing.
	 * @return image information
	 * @public
	 * @since 1.1
	 */
	public function Image($file, $x='', $y='', $w=0, $h=0, $type='', $link='', $align='', $resize=false, $dpi=300, $palign='', $ismask=false, $imgmask=false, $border=0, $fitbox=false, $hidden=false, $fitonpage=false, $alt=false, $altimgs=array()) {

At least FPDF has a manual. Luckily that same manual applies largely to TCPDF - although not the extras that TCPDF has. The most useful addition is the tcpdf::WriteHTML($html_string) method which renders html markup as PDF. I haven't used that facility much so I'm not sure how well it uses CSS.

Other improvements are better rendering of non-latin text (eg Arabic) and the ability to embed SVG graphics.

 

Link to comment
Share on other sites

6 minutes ago, ginerjm said:

Are you wanting to create a pdf document or a pdf image of an html page?  IMHO the latter is not an easy task with FPDF.

I am wanting to create a pdf document.

Basically a branded document that pulls data from the database and builds a quote that would be sent to a client.

Link to comment
Share on other sites

Ok then.  FPDF is very good at producing a document of data/text from a db or any other source.  You basically have to have a plan and you build it row by row as if you were working on an x/y grid where (usually) you write out 'cells' across a row.  Works very well if you are trying to output a table view of your data but also for a lot of other things.  I have used it to produce drawsheets for my handball events if you can envision what that looks like.

Link to comment
Share on other sites

6 minutes ago, ginerjm said:

Ok then.  FPDF is very good at producing a document of data/text from a db or any other source.  You basically have to have a plan and you build it row by row as if you were working on an x/y grid where (usually) you write out 'cells' across a row.  Works very well if you are trying to output a table view of your data but also for a lot of other things.  I have used it to produce drawsheets for my handball events if you can envision what that looks like.

very useful info - thanks for that - i could well be back here for help but ill have a stab.

Link to comment
Share on other sites

  • 1 month later...

My older version of TCPDF just collapsed like a house of cards when I tried and example page with PHPv8 throwing out errors everywhere - so that goes in the bin. As far as I know the newer versions use composer for installation.

I did manage a sample using TFPDF

image.png.0e6e79acd82a36f895c550460480e577.png

Code

<?php
const ROOT = 'c:/inetpub/wwwroot';
require(ROOT.'/tfpdf/tfpdf.php');
include('db_inc.php');

$db = pdoConnect('exams');


$res = $db->query("SELECT name_en as en
                        , name_ar as ar 
                   FROM staff
                   ORDER BY RAND()
                   LIMIT 10
                   ");
$rows = $res->fetchAll();



class testpdf extends TFPDF {
    
    public function makePage($rows)
    {
        $this->AddPage();
        
        $this->setFont('', '', 10);
        foreach ($rows as $r) {
            $this->SetFont('arial','',10);
            $this->Cell(60, 10, $r['en'], 'TB', 0, 'L');
            $this->SetFont('calibri','',10);
            $this->Cell(60, 10, rtl($r['ar']), 'TB', 1, 'R');
        }
    }
}
 
$test = new testpdf();
$test->AddFont('calibri','','calibri.ttf',true);

$test->makePage($rows);

$test->output();

function rtl($str)
{
    $k = mb_strlen($str);
    for ($i=0; $i<$k; $i++) $a[] = mb_substr($str, $i, 1);
    $a = array_reverse($a);
    return join('', $a);
}
 
?>

 

  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.