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"
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!
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.
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.
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!
Related
Previously i was working with dompdf in laravel application to generate invoices.. Its taking time in generating invoices but working perfectly . Below is the code of dompdf to generate invoice by just sending the view.
PDF::setOptions(['dpi' => 150, 'defaultFont' => 'sans-serif']);
// pass view file
view()->share('account_invoice',$account_invoice);
view()->share('account_invoice_item',$account_invoice_item);
$pdf = PDF::loadView('admin/invoice/InvoiceTemplate/template');
But now i am working with FPDF. Is there anything like dompdf to pass view to generate PDF.
$pdf = new FPDF();
View::make('admin/invoice/InvoiceTemplate/template');
I know my code is incorrect for FPDF but any idea how can i pass view to FPDF to generate pdf so i can send it by attactment.
It looks like your goal is to dump raw html onto the pdf and have it be formatted accordingly? Take a look at this tutorial, it might help, but I'd actually recommend a different approach.
Basically, fpdf is has no html writer built in, at least none as far as I'm aware. So I'd recommend a pdf writer with better out of the box support for your needs. Take a look at Snappy and wkhtmltopdf. With Snappy and wkhtmltopdf you can pretty easily generate pdfs on the fly by passing it html, as you're attempting to in your examples.
<?php
use Knp\Snappy\Pdf;
$snappy = new Pdf('/path/to/wkhtmltopdf');
$snappy->generateFromHtml(View::make('admin/invoice/InvoiceTemplate/template'), '/tmp/invoice.pdf');
So I'm trying to create a PDF invoice for a garage and I have the below currently (I know the variables are missing but they had personnel info in for the testing on localhost as well as some of the blank text in cells). So anyway a few things!
I want the Work and the corresponding amount on the same line which I have done some research on and it still wont work as I don't fully understand it, is someone able to help on how it works so I can implement it?
The idea is to create the invoice saving it to a SQL database, which I have done, with the added option to either print the document straight out or send via e-mail to the customer is it possible to Save the PDF to send as an attach through PHPMailer(Which I will be getting to grips with soon!). Or print straight from this?
Ideally wouldn't want to PDF to clog up after creating.
Any help would be greatly appreciated, need anything give me a shout!
<?php
require ('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','',24);
//Cell (Width, height, text, border, end line, 'align')
$pdf->Cell(189,25,'Cheshire West Vehicle Ltd Invoice', 0,1, 'C');
$pdf->SetFont('Arial','',14);
//CWV & Customer Address
$pdf->Cell(65,5,'',0,0);
$pdf->Cell(124, 5, $firstname.' '.$lastname, 0, 1, 'R');
$pdf->Cell(40,5,'',0,0);
$pdf->Cell(149 ,5, $housenumber.', '.$postcode, 0 ,1, 'R');
$pdf->Cell(30,5,'',0,0);
$pdf->Cell(159,5,$contactnumber,0,1,'R');
$pdf->Cell(30,5,'',0,0);
$pdf->Cell(159,5,$email,0,1,'R');
//Date
$pdf->SetFont('Arial','',12);
$pdf->Cell(189,15,$date,0,1,'R');
$pdf->SetFont('Arial','',13);
//Vehicle Stuff
$pdf->Cell(12,15,'VRM: ',0,0);
$pdf->Cell(29,15,$vrm,0,0,'C');
$pdf->Cell(13,15,'Make: ',0,0);
$pdf->Cell(35,15,$make,0,0,'C');
$pdf->Cell(14,15,'Model: ',0,0);
$pdf->Cell(44,15,$model,0,0,'C');
$pdf->Cell(18,15,'Mileage: ',0,0);
$pdf->Cell(24,15,$mileage,0,1,'C');
//Work
$pdf->Cell(149,5,'Work Carried Out',1,0,'C');
$pdf->Cell(40,5,'Amount',1,0,'C');
$pdf->Cell(0,5,'',0,1);
$pdf->MultiCell(189,25,$work1,1,'C',false);
$pdf->MultiCell(40,25,$amount1,1,1);
$pdf->MultiCell(189,25,$work2,1,'C',false);
$pdf->MultiCell(40,25,$amount2,1,1);
$pdf->MultiCell(189,25,$work3,1,'C',false);
$pdf->Cell(40,25,$amount3,1,1);
//Other
$pdf->Cell(189,5,'Other Details',1,'C');
$pdf->MultiCell(189,25,$other,1,'C',false);
$pdf->Output();
?>
Yes. If you look at the docs for tcpdf, you'll see there are options for output, including one (S) that returns the PDF data directly as a string, rather than writing it to a file. You can pass this raw data directly into PHPMailer's addStringAttachment() method, like this:
$pdfdata = $pdf->Output('name.pdf', 'S');
$mail->addStringAttachment($pdfdata, 'name.pdf');
It will get its encoding and MIME type set automatically.
In other news, the tcpdf docs are hopeless to navigate.
I need to export html page to pdf file with everything that's written in it, after I press submit button. It will open new page, with info, and I need for script to automatically make .pdf file (already uploaded to webserver), and get the link from file. Could you give me some easy example (if available, without any plugins, or other features that I must download, I would prefer clean PHP).
Just try this
HTML to PDF with PHP
Using open-source HTML2FPDF project
This solution uses HTML2PDF project (sourceforge.net/projects/html2fpdf/). It simply gets a HTML text and generates a PDF file. This project is based upon FPDF script (www.fpdf.org), which is pure PHP, not using the PDFlib or other third party library. Download [HTML2PDF][1], add it to your projects and you can start coding.
Code example
require("html2fpdf.php");
$htmlFile = "your link";
$buffer = file_get_contents($htmlFile);
$pdf = new HTML2FPDF('P', 'mm', 'Letter');
$pdf->AddPage();
$pdf->WriteHTML($buffer);
$pdf->Output('test.pdf', 'F');
I have a website now and I want to create a button on it to convert this page to PDF.
Is there any code to make this happen? I cannot find it on the internet.
So I want to have a button and when I press on it it converts the page to a .PDF file.
I do not want to use a third party website to generate the PDF's. I want to use it for internal purposes to generate files with PHP. So I need the code what can make a PDF for each page.
I use wkhtmltopdf - works very well - http://code.google.com/p/wkhtmltopdf/ there is a PHP wrapper
Updated based on comments below on usage :
How to use the integration class:
require_once('wkhtmltopdf/wkhtmltopdf.php'); // Ensure this path is correct !
$html = file_get_contents("http://www.google.com");
$pdf = new WKPDF();
$pdf->set_html($html);
$pdf->render();
$pdf->output(WKPDF::$PDF_EMBEDDED,'sample.pdf');
Use FPDF. It's a well-respected PDF-generating library for PHP that is written in pure PHP (so installing it should be dead simple for you).
Try this:
http://www.macronimous.com/resources/Converting_HTML2PDF_using_PHP.asp
It will convert HTML to a PDF using FPDF and HTML2PDF class.
Also found this:
http://www.phpclasses.org/package/3168-PHP-Generate-PDF-documents-from-HTML-pages.html
how generate PDF document with MYSQL query more than 15 fields with wrap text? I have generated PDF with fpdf option. but I have created only 8 fields with nowrap text. so any one please solve the solutions. thanks.
For example. I have created with nowrap text.
$pdf=new PDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',12);
$pdf->SetTextColor(4,126,167);
$pdf->Cell(10,10,'Worksheet Report');
$pdf->Ln();
//Column titles
$header=array('Client','Team In Charge','Staff In Charge','Priority','Master Activity','Sub Activity','Last Reports Sent','Job in Hand','Team Incharge Notes','External Due Date','Befree Due Date','Status');
$pdf->SetFont('Arial','B',6);
$pdf->FancyTable($header);
$pdf->SetFont('Arial','',5);
$pdf->SetFillColor(255,255,255);
$pdf->SetTextColor(0);
$pdf->SetLineWidth(.2);
$sql=$_SESSION['query'];
$result = mysql_query($sql);
while($row = #mysql_fetch_array($result)){
$pdf->Cell(15,10,htmlspecialchars($row["lp_wrk_CompanyName"]),1,0,'T',true);
$pdf->Cell(17,10,$commonUses->getFirstLastName($row["wrk_TeamInCharge"]),1,0,'T',true);
$pdf->Cell(17,10,$commonUses->getFirstLastName($row["wrk_StaffInCharge"]),1,0,'T',true);
$pdf->Cell(12,10,htmlspecialchars($row["lp_wrk_priority"]),1,0,'T',true);
$pdf->Cell(17,10,htmlspecialchars($row["lp_wrk_MasCode"]).($row["lp_wrk_MasCode"]!=""? "-":"").htmlspecialchars($row["lp_wrk_MasterActivity"]),1,0,'T',true);
$pdf->Cell(15,10,htmlspecialchars($row["lp_wrk_SubCode"]).($row["lp_wrk_SubCode"]!=""? "-":"").htmlspecialchars($row["lp_wrk_SubActivity"]),1,0,'T',true);
$pdf->Cell(19,10,htmlspecialchars($row["wrk_Details"]),1,0,'T',true);
$pdf->Cell(18,10,htmlspecialchars($row["wrk_Notes"]),1,0,'T',true);
$pdf->Cell(19,10,htmlspecialchars($row["wrk_TeamInChargeNotes"]),1,0,'T',true);
$pdf->Cell(18,10,$commonUses->showGridDateFormat($row["wrk_DueDate"]),1,0,'T',true);
$pdf->Cell(18,10,$commonUses->showGridDateFormat($row["wrk_InternalDueDate"]),1,0,'T',true);
$pdf->Cell(15,10,htmlspecialchars($row["lp_wrk_Status"]),1,0,'T',true);
$pdf->Ln();
}
There's a webservice called DocRaptor that uses Prince to generate pdf and xls from html. They offer a free plan as well as larger paid plans.
check this web link :-
http://qualitypoint.blogspot.com/2010/07/resolved-wrap-text-issue-in-fpdf-table.html
http://blog.themeforest.net/tutorials/how-to-create-pdf-files-with-php/
I hope it is useful for you :)
Have a look at the dompdf class. It generates the pdf from your html code so it's much easier and quicker to get the results you want. It's still a bit finicky with floats so you have to imagine your are in the 90's and do a table based design.
My advice is to print out the table in html, than convert in into pdf: is easier, faster, and more flexible: when you have the html documents, will be easy (for example) convert into a jpg, if you'll need to in the future.
If you can install software on your server, i raccomend you wkhtmltopdf.
You generate the html file, and wkhtmltopdf convert it to pdf, with full (webkit based) css support.
Else, i've found that html2ps doeas a great job, but is quite slower with large files.. css support is good, but no guarantee that it will support css3, for example.