Reputation: 25
I am trying to make a document that will be viewed as a PDF online. An example URL would be, url.com/PDF.php?name=John Doe
.
The PDF will contain a lot of predefined text, and I want to echo the part after name=
into the PDF on multiple occasions.
I've read (and tried) FPDF, TCPDF but I cant figure out how to implement this.
<?php
echo htmlentities($_GET["name"]); //John
?>
Does anybody know a straight-forward way to achieve this?
I have no PDF code as I havent gotten either FPDF or TCPDF to properly work/display what I am doing. Thought I would check if this is the proper way to go or if i should use pure php.
A 'for dummies' guide to make these php/pdf libraries work would actually be great. I download the library, upload it to my server, duplicate one of the files from the examples folder, change the routing to (tcpdf-relative) files, but no, it will not work. If i go to one of the files in examples/example_001.pdf it displays correctly. If I copy that file to root and change links to match the new location it doesnt work.
Example works great http://fragger.no/pdf/examples/example_003.php
This file which is placed in root does not display at all, only a white page. No errors.
<?php
// Include the main TCPDF library (search for installation path).
require_once('examples/tcpdf_include.php');
// Extend the TCPDF class to create custom Header and Footer
class MYPDF extends TCPDF {
//Page header
public function Header() {
// Logo
$image_file = K_PATH_IMAGES.'logo_example.jpg';
$this->Image($image_file, 10, 10, 15, '', 'JPG', '', 'T', false, 300, '', false, false, 0, false, false, false);
// Set font
$this->SetFont('helvetica', 'B', 20);
// Title
$this->Cell(0, 15, '<< TCPDF Example 003 >>', 0, false, 'C', 0, '', 0, false, 'M', 'M');
}
// Page footer
public function Footer() {
// Position at 15 mm from bottom
$this->SetY(-15);
// Set font
$this->SetFont('helvetica', 'I', 8);
// Page number
$this->Cell(0, 10, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
}
}
// create new PDF document
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Nicola Asuni');
$pdf->SetTitle('TCPDF Example 003');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// set some language-dependent strings (optional)
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
require_once(dirname(__FILE__).'/lang/eng.php');
$pdf->setLanguageArray($l);
}
// ---------------------------------------------------------
// set font
$pdf->SetFont('times', 'BI', 12);
// add a page
$pdf->AddPage();
// set some text to print
$txt = <<<EOD
TCPDF Example 003
Custom page header and footer are defined by extending the TCPDF class and overriding the Header() and Footer() methods.
EOD;
// print a block of text using Write()
$pdf->Write(0, $txt, '', 0, 'C', true, 0, false, false, 0);
// ---------------------------------------------------------
//Close and output PDF document
$pdf->Output('pdf.pdf', 'I');
//============================================================+
// END OF FILE
//============================================================+
I edited this: // Include the main TCPDF library (search for installation path). require_once('examples/tcpdf_include.php');
To // Include the main TCPDF library (search for installation path). require_once('tcpdf.php');
and it worked, so now all i need is to use GET information in the document.
This is where I'd love to display ?name=
// set font
$pdf->SetFont('helvetica', '', 12);
// add a page
$pdf->AddPage();
// set some text to print
$txt = <<<EOD
My text
**NAME VALUE**
Some more text
EOD;
Upvotes: 1
Views: 1493
Reputation: 10381
First of all, in order to use $_GET
you have to check if it exists :
<?php
if ( isset( $_GET[ "name" ] ) )
$name = $_GET[ "name" ]; // ◄■■■■■■ NAME FROM URL.
else $name = "noname"; // ◄■■■■■■ "NONAME" OR "?" OR ANYTHING ELSE.
// Include the main TCPDF library (search for installation path).
require_once('examples/tcpdf_include.php');
.
.
.
The name is stored in $name
, so you can use that variable anywhere :
$txt = <<<EOD
My text
$name
Some more text
EOD;
Upvotes: 1