MOCKSTACKS
EN
Questions And Answers

More Tutorials









PHP Create PDF files



This code requires that you use the PDFlib library for it to function properly.

<?php
$pdf = pdf_new(); //initialize new object

pdf_begin_document($pdf); //create new blank PDF
   pdf_set_info($pdf, "Author", "John Doe"); //Set info about your PDF
   pdf_set_info($pdf, "Title", "HelloWorld");
     pdf_begin_page($pdf, (72 * 8.5), (72 * 11)); //specify page width and height
       $font = pdf_findfont($pdf, "Times-Roman", "host", 0) //load a font
       pdf_setfont($pdf, $font, 48); //set the font
       pdf_set_text_pos($pdf, 50, 700); //assign text position
       pdf_show($pdf, "Hello_World!"); //print text to assigned position
     pdf_end_page($pdf); //end the page
 pdf_end_document($pdf); //close the object

$document = pdf_get_buffer($pdf); //retrieve contents from buffer

$length = strlen($document); $filename = "HelloWorld.pdf"; //Finds PDF length and assigns file name

header("Content-Type:application/pdf");
header("Content-Length:" . $length);
header("Content-Disposition:inline; filename=" . $filename);

echo($document); //Send document to browser
unset($document); pdf_delete($pdf); //Clear Memory
?>


Conclusion

In this page (written and validated by ) you learned about PHP Create PDF files . What's Next? If you are interested in completing PHP tutorial, your next topic will be learning about: PHP YAML.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.