Convert HTML form data into a PDF file using PHP
Asked Answered
T

4

10

I have been looking and testing this for a couple days now and was wondering if anyone could point me in a different direction. I have a very long job application HTML form (jobapp.html) and a matching PDF (jobpdf.pdf) that have the same field names for all entries in both the HTML form and the PDF. I need to take the user data that is entered in the form and convert it to a PDF. This is what I have gathered so far but don't know if I am on track:

Is pdftk the only viable 3rd party app to accomplish this?

Using pdftk would i take the $_POST data collected for the user and generate a .fdf(user.fdf) then flatten the .fdf on the .pdf(job.pdf). So irreguarless of where the fields are located on each document the information on the fdf would populate the pdf by field names?

I have been trying http://koivi.com/fill-pdf-form-fields/tutorial.php

I have also looked at "Submit HTML form to PDF"

Thant answered 17/8, 2012 at 17:8 Comment(6)
When you say "capture" do you mean take a client-side screen capture and turn that into a PDF? I imagine it's going to be a lot easier to create a template PDF and just merge the form data into it. It's certainly a more standard approach to creating reports like this.Danelle
It is essentially an job application that is filled out and when the user submits the form I want to be able to take and save the application in a pdf.Thant
I would treat the PDF as a "view" just like the page. Both have fields which are bound to data, and otherwise don't know about each other or depend on each other in any way. The HTML "view" has its field laid out for browser rendering and interaction, the PDF "view" has its fields laid out for printability as a kind of report on the data.Danelle
@Danelle Thanks for the insight. So would I have to generate a fdf from the html form after the user entered the info and submitted it, then merge the fdf with the pdf? I am assuming the pdf needs to have fields names that mirror the html form. Is the best way to merge these with pdftk?Thant
I don't know what an FDF is nor do I know anything about pdftk, so I can't really advise on the specific implementation. Just as a general guiding principle that creating two distinctly separate views is easier to maintain than having a second view somehow tightly bound to the first (or even trying to be generated from the client-side view of the first).Danelle
I added an example to my answer below. It's very easy!Cailly
C
8

I have used fpdf several times to create php-based pdf documents. An example following:

require('fpdf.php');

$pdf = new FPDF();

$pdf->AddFont('georgia', '', 'georgia.php');
$pdf->AddFont('georgia', 'B', 'georgiab.php');
$pdf->AddFont('georgia', 'I', 'georgiai.php');

# Add UTF-8 support (only add a Unicode font)
$pdf->AddFont('freesans', '', 'freesans.php', true);
$pdf->SetFont('freesans', '', 12);

$pdf->SetTitle('My title');
$pdf->SetAuthor('My author');
$pdf->SetDisplayMode('fullpage', 'single');

$pdf->SetLeftMargin(20);
$pdf->SetRightMargin(20);

$pdf->AddPage();
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();

You can learn very fast with these tutorials from the website itself.


EDIT: Example to save form data: (yes, is very easy...)

require('fpdf.php');
$pdf = new FPDF();

$pdf->AddPage();
foreach ($_POST as $key =>$data)
{
    $pdf->Write(5, "$key: $data"); //write
    $pdf->Ln(10); // new line
}
$pdf->Output($path_to_file . 'file.txt','F'); // save to file

Look at these pages created with fpdf, really!

enter image description here

enter image description here

Cailly answered 17/8, 2012 at 17:17 Comment(3)
@djacksway just use POST data to write it in fpdf. like $pdf->Cell(40,10,$_POST['username]);etc... It's very easy.Cailly
would this not be cumbersome for 100 or so fields which then would have to have the information that related to the field written outThant
@djacksway did you saw my edited example? foreach ($_POST as $key =>$data). You must be careful adding pages each certain amount of $pdf->Write(5, "$key: $data"); I think this is enough to start your solution...Cailly
N
2

http://www.fpdf.org/

That would be the library to do it. I used it here to add images to a form and submit it to create a PDF with those images: http://productionlocations.com/locations

The actual code to do it is pretty complex.

Nightspot answered 17/8, 2012 at 17:10 Comment(0)
R
1

I have found PrinceXML very easy to use. It takes your HTML/XML, applies CSS, and converts it into a PDF. The PHP extensions work very well. Unfortunately, it's not free.

Rebeccarebecka answered 17/8, 2012 at 17:22 Comment(0)
A
1

One way you can consider is using an online API that converts any HTML to PDF. You can send them a generated HTML (easier to produce) that will contains your user's submitted data, and receive back a high fidelity PDF.

There are quite a few services available on the market. I like to mention PDFShift because it offers a package in PHP that simplifies the work for you.

Once you've installed it (using Composer, or downloaded it directly, depending on your choices) you can quickly convert an HTML document like this:

require_once('vendor/autoload.php');
use \PDFShift\PDFShift;

PDFShift::setApiKey('{your api key}');
PDFShift::convertTo('https://link/to/your/html', null, 'invoice.pdf');

And that's it. There are quite a few features you can implement (accessing secured documents, adding a watermark, and more).

Hope that helps!

Apophysis answered 1/6, 2018 at 12:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.