How to Generate PDFs with PHP: New Premium Tutorial
plus

How to Generate PDFs with PHP: New Premium Tutorial

Tutorial Details
  • Premium Exclusive
  • Difficulty: Intermediate
  • Format: Tutorial + Screencast

PDFs may well be the best format for distributing documents on the web. In today’s tutorial and screencast, I’ll show you how you can generated PDFs with PHP. Help give back to Nettuts+ by becoming a Premium member!

Example PDF Created with PHP!

By subscribing and becoming a Premium member, you’ll gain access to the best tutorial, screencasts, and freebies that every Tuts sites has to offer – all for a measly $19 a month.

Join Net Premium

NETTUTS+ Screencasts and Bonus Tutorials

For those unfamiliar, the family of TUTS sites runs a premium membership service. For $19 per month, you gain access to exclusive premium tutorials, screencasts, and freebies from Nettuts+, Psdtuts+, Aetuts+, Audiotuts+, and Vectortuts+! For the price of a pizza, you’ll learn from some of the best minds in the business. Help give back to Nettuts+ by becoming a Premium member!

Tags: Premium
Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • Rohan Mehta

    Awesome!! I’ve been waiting for something like this!

  • Eric

    Great Job Andrew this was a wonderful tutorial, eh?

    I grew up in Michigan watching CBC as a kid so it is nice to hear the great canuck accent.

  • dave

    Thanks for tutorial!

    I want to make a note about margin usage. While you do provide a parameter to allow changing the margin amount, you do not compensate for the parameter and effectively hard code your margins.

  • Arnaud

    I’m wondering to get a premium account for this tutorial…May I ask you which way you generate the PDF ? (FPDF,…)
    Thanks a lot !

  • http://www.foxyturkey.com Yigit Ozdamar

    Nice! I really love tuts like this one!

    Thanks!

  • http://www.jeffadams.co.uk Jeff Adams

    Okay, so I cancelled my membership about 3-4 months back – nothing to do with the site, more that I had zero time to spend checking out the tuts.

    This is one tut I have been really wanting for so long, I’m going to joiin back this weekend lol

  • http://www.nouveller.com/ Benjamin Reid

    Is this using a library like http://www.fpdf.org/ or is it totally from scratch?

    • http://www.nouveller.com/ Benjamin Reid

      Can anything be done about the styling of the anchor tags on the new NETTUTS design? It’s barely noticeable against normal text. Either change the colour or add an underline.

      • http://laranz.com lawrence77

        I second that Benjamin :D

      • Ryan

        Yeah, I’ve always been a proponent of underlined links. Much easier to see. I also like dotted underline links. I reckon they might change it!

    • http://laranz.com lawrence77

      which one u want? :D

    • dave

      Andrew uses the fpdf library in the screencast.

      • http://www.nouveller.com/ Benjamin Reid

        Thanks dave, no need to download then. ;)

  • http://www.desenweb.es Francesc

    lol…. esto es un tutorial premium? si existen un montón de soluciones gratis en la web. En que se diferencia?

    • http://www.3l1r.com Braden Keith

      Not all the tutorials on the web would cover it from scratch though. They assume a lot of knowledge. It also helps to get it from another person’s perspective with different wording. People following along several tutorials of the same format may not understand until he says something a particular way, and it all clicks.

  • http://89.is Chris

    Looks awsome, will try it out later.

    Anyway of generating a random code that could be used within the invoice as a Ticket number?

    I was looking for a simple payment system that would allow users to buy a eTicket then once the payment has gone through they get sent a PDF which they can print out and use as a ticket/invoice

  • http://spotdex.com Davidmoreen

    I was flipping though my php reference book the other day, and I noticed that there were a lot of functions for pdf actions.

    Great tutorial to start off learning how to dynamically make PDFs.

  • fatih

    i am not a plus member yet so i want to ask:
    is it explained in the video how to fill a predefined pdf form from a database using php??
    thanks.

    • dave

      Andrew shows how to use the basics of the fpdf library and so does not talk about loading an existing pdf file and writing to it.

  • Ryan

    Cool! Creating a PDF is kinda hard but a lot of clients like to be able to do it. I think this tutorial could be pretty good!

  • http://www.jordanwalker.net Jordan Walker

    It is really important to know how to generate .pdf files with PHP, even if Adobe Reader has zero day exploits!

  • Elias

    Thank you, This is just what I was looking for.

  • Mark

    Nice and clearly explained tutorial, but seems a painful process to create a pdf, reminds me of the old ‘green screen’ typesetting days!

  • VladLozM

    I like to use mPDF [http://mpdf.bpm1.com/]

    Greetings!

    :D

  • http://www.compasscreative.ca/ Jonathan

    A much needed tutorial, thanks for your work in this. I actually had to learn this on my own about 2 weeks ago. I opted to use TCPDF (http://www.tecnick.com/public/code/cp_dpage.php?aiocp_dp=tcpdf) instead, which I believe is actually a fork off of FPDF. It just seemed a little more maintained.

    Andrew – a little trouble typing that day? ;)

  • janM

    If you want to add things to an existing pdf-file you need fpdi in combination with fpdf – just look on fpdf.org an then search for “partners” oder “links” or something like that – there you’ll find fpdf.
    I’m using this for a long time – it’s much simpler: you can upload an nice designed pdf-file and just add some details dynamicly via php.

  • http://freddygonzalez.me Freddy Gonzalez

    Not so great . . . I had fun with the first part but as soon as you started using functions, classes and objects? you lost me,,,

    • http://freddygonzalez.me Freddy Gonzalez

      I think I may understand it…. is this how the class thing works?

      http://freddygonzalez.me/dev/pdf/aLittleHelp.lasso

      • http://andrewburgess.posterous.com Andrew Burgess
        Author

        Those functions that we’re calling within the constructor could be called anywhere within the class or even after you create an instance of the class. In the PDF_reciept class, I wanted to set the page margins when we created the instance; I’m pretty sure you could use those functions again after creating the instance to change the margins. I’ve set a default value for the margins so that you could leave that value out when creating the instance.

        Hope that helps!

  • http://www.soamazing.eu Rick

    Great tut! Need this to make dynamic reports from a php system. I was just wondering about the method used in this tut (I’m not a plus member…yet…)

    I’ve been investigating in this and what I learned is that php comes with a (few) inbuild pdf functions which you can activate when you have full control of your hosting envoirment (http://php.net/manual/en/book.pdf.php)… something I don’t have… so I need a library or something I can just install on my hosting. If available, maybe one that is a library for codeigniter?

    Anyway, can somebody let me know if this tut uses an inbuild php function or that it uses a library you can easily install on your hosting (without access to the php settings)? If it’s the last then I need to get myself a plus account! :-)

    Thanks!

    • http://andrewburgess.posterous.com Andrew Burgess
      Author

      I use the FPDF library in this tut.

  • Nuj

    I want to see the tutorial but I’m not a Member can someone send a copy of it on my email junliwanag@ymail.com

    Thanks

  • http://www.nunomedia.com Nuno

    I actually mentioned with a friend less than 4 hours ago about something like that. Thanks guys…

  • andrew

    So, is this tut better than available examples at homepage of fpdf or any other found on the net?

  • Mike

    Nice.. This is definitly worth for a new subscription. Thx ;)
    Now, I hope a great XML read and write with PHP will be there soon too ;)

  • http://arunmvishnu.com/ Arun
  • http://ibad.bebasbelanja.com Ibad

    I use fpdf and fpdi, it’s more simple :D

  • http://www.bigtunainteractive.com Adam Hermsdorfer

    I have been looking for a script to generate PDF’s forever. Last straw – I’m going to sign up for premium membership.

  • http://newelementdesigns.com Don
    • http://www.dompdf.com Ryan Masten

      The correct site is http://www.dompdf.com as Benj is no longer doing the development on it, we are.
      We integrate with FPDF to provide additional features.

      -Ryan H. MAsten

  • Ron

    Awesome, this is exactly what I’ve been looking to do. Now I just need to know how to automatically email the generated pdf file as an attachment.

  • http://www.cssispoetry.com blake

    Ohh Shizz, nice tutorial Adam. =)

  • Deoxys

    Nice tutorial, but pleeeaase, practice more on your typing. That’s so nasty ;-)