Before I proceed to show you how to include some basic images into a given PDF document, first I'd like you to recall properly how to use the handy "set_text_pos()" and "continue_text()" methods that I explained in the previous article of the series, in order to build PDF files that contain multiple lines of text. Regarding the utilization of these methods, below I listed a pair of illustrative examples that show how to implement them. Here are the corresponding code samples, so have a look at them, please: // example creating a basic PDF document with PHP and multiple try { // create new instance of the 'PDFlib' class $pdf=new PDFlib(); // open new PDF file if(!$pdf->begin_document("","")){ throw new PDFlibException("Error creating PDF document. ".$pdf- } $pdf->set_info("Creator","example.php"); $pdf->set_info("Author","Alejandro Gervasio"); $pdf->set_info("Title","Example on using PHP to create PDF $pdf->begin_page_ext(421,595,"");
$font=$pdf->load_font("Helvetica-Bold","winansi",""); $pdf->setfont($font,24.0); $pdf->set_text_pos(50,500); $pdf->show("PHP is great for creating PDFs!"); $pdf->continue_text('This is another line of text'); $pdf->continue_text('This is another line of text'); $pdf->continue_text('This is another line of text'); // end page $pdf->end_page_ext("");
// end document $pdf->end_document(""); // get buffer contents $buffer=$pdf->get_buffer(); // get length of buffer $len=strlen($buffer); // display PDF document header("Content-type: application/pdf"); header("Content-Length: $len"); header("Content-Disposition: inline; filename=example.pdf"); echo $buffer; } catch (PDFlibException $e){ echo 'Error Number:'.$e->get_errnum()."n"; echo 'Error Message:'.$e->get_errmsg(); exit(); } // example creating a basic PDF document with PHP and multiple try { // create new instance of the 'PDFlib' class $pdf=new PDFlib(); // open new PDF file if(!$pdf->begin_document("","")){ throw new PDFlibException("Error creating PDF document. ".$pdf- } $pdf->set_info("Creator","example.php"); $pdf->set_info("Author","Alejandro Gervasio"); $pdf->set_info("Title","Example on using PHP to create PDF $pdf->begin_page_ext(421,595,"");
$font=$pdf->load_font("Helvetica-Bold","winansi",""); $pdf->setfont($font,24.0); $pdf->set_text_pos(50,500); $pdf->show("PHP is great for creating PDFs!"); $pdf->set_text_pos(50,450); $pdf->show('This is another line of text'); $pdf->set_text_pos(50,400); $pdf->show('This is another line of text'); $pdf->set_text_pos(50,350); $pdf->show('This is another line of text'); // end page $pdf->end_page_ext("");
// end document $pdf->end_document(""); // get buffer contents $buffer=$pdf->get_buffer(); // get length of buffer $len=strlen($buffer); // display PDF document header("Content-type: application/pdf"); header("Content-Length: $len"); header("Content-Disposition: inline; filename=example.pdf"); echo $buffer; } catch (PDFlibException $e){ echo 'Error Number:'.$e->get_errnum()."n"; echo 'Error Message:'.$e->get_errmsg(); exit(); } As you can see, the two examples above demonstrate how to utilize the "set_text_pos()", "show()", and "continue_text()" methods that come integrated with the PDFlib library to build a couple of PDF documents that include some basic multi-line texts. Of course, in this case, the "set_text_pos()" method makes it really easy to display a given string in a PDF file using a pair of X, Y coordinates, which can be expanded to scope two or more lines of text. All right, at this stage you've hopefully recalled the complete sequence of steps required to build basic PDF files that contain simple strings. Considering this, I believe that it's time to continue learning more methods that come bundled with the PDFlib package. In the upcoming section of this tutorial I'm going to show you how to incorporate a simple image into an existing PDF file to make it slightly more appealing. Of course, to see how this will be done, you have to click on the link below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|