Can I pass my view to FPDF to generate PDF? - php

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');

Related

Pimcore how to create PDF and send via email

Anybody can give us a hint how to treat this best, if this can be achieved with „standards“ of pimcore?
Render a PDF with dynamic content and merge it with a PDF which has legal information.
Then send final pdf via email.
The PDF should be able to designed with HTML
You can use the Web2Print Documents to create and render the first PDF with dynamic content. To modify the PDF afterwards, you can hook into the PDF generation process (Pimcore 4 example):
\Pimcore::getEventManager()->attach("document.print.postPdfGeneration", function (\Zend_EventManager_Event $e) {
$document = $e->getTarget();
$pdf = $e->getParam("pdf");
Once you got that PDF, you can merge it with other PDFs like described here: Merge PDF files with PHP
Sending it via email should be easy at this point.

Generate PDF from HTML/CSS/PHP in Symfony 1.4

I am working on a Symfony 1.4 project. I need to make a PDF download link for a (yet to be) generated voucher and I have to say, I am a bit confused. I already have the HTML/CSS for the voucher, I created the download button in the right view, but I don't know where to go from there.
Use Mpdf to create the pdf file
http://www.mpdf1.com/
+1 with wkhtmltopdf
I'd even recommand the snappy library.
If you use composer, you can even get the wkhtmltopdf binaries automatically
Having used wkhtmltopdf for a while I've moved off it as 1) it has some serious bugs and 2) ongoing development has slowed down. I moved over to PhantomJS which is proving to be much better in terms of functionality and effectiveness.
Once you've got something like wkhtmltopdf or PhantomJS on your machine you need to generate the HTML page and pass that along to it. I'll give you an example assuming you use PhantomJS.
Initially set what every request parameters you need to for the template.
$this->getRequest->setParamater([some parameter],[some value]);
Then call the function getPresentation() to generate the HTML from a template. This will return the resulting HTML for a specific module and action.
$html = sfContext::getInstance()->getController()->getPresentation([module],[action]);
You'll need to replace the relative CSS paths with a absolute CSS path in the HTML file. For example by running preg_replace.
$html_replaced = preg_replace('/"\/css/','"'.sfConfig('sf_web_dir').'/css',$html);
Now write the HTML page to file and convert to a PDF.
$fp = fopen('export.html','w+');
fwrite($fp,$html_replaced);
fclose($fp)
exec('/path/to/phantomjs/bin/phantomjs /path/to/phantomjs/examples/rasterize.js /path/to/export.html /path/to/export.pdf "A3");
Now send the PDF to the user:
$this->getResponse()->clearHttpHeaders();
$this->getResponse()->setHttpHeader('Content-Description','File Transfer');
$this->getResponse()->setHttpHeader('Cache-Control','public, must-revalidate, max-age=0');
$this->getResponse()->setHttpHeader('Pragma: public',true);
$this->getResponse()->setHttpHeader('Content-Transfer-Encoding','binary');
$this->getResponse()->setHttpHeader('Content-length',filesize('/path/to/export.pdf'));
$this->getResponse()->setContentType('application/pdf');
$this->getResponse()->setHttpHeader('Content-Disposition','attachment; filename=export.pdf');
$this->getResponse()->setContent(readfile('/path/to/export.pdf'));
$this->getResponse()->sendContent();
You do need to set the headers otherwise the browser does odd things. The filename for the generated HTML file and export should be unique to avoid the situation of two people generating PDF vouchers at the same time clashing. You can use something like sha1(time()) to add a randomised hash to a standard name e.g. 'export_'.sha1(time());
Use wkhtmltopdf, if possible. It is by far the best html2pdf converter a php coder can use.
And then do something like this (not tested, but should be pretty close):
public function executeGeneratePdf(sfWebRequest $request)
{
$this->getContext()->getResponse()->clearHttpHeaders();
$html = '*your html content*';
$pdf = new WKPDF();
$pdf->set_html($html);
$pdf->render();
$pdf->output(WKPDF::$PDF_EMBEDDED, 'whatever_name.pdf');
throw new sfStopException();
}

Convert HTML form data into a PDF file using PHP

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!

Webpage convert to PDF button

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

Creating a new PDF by Merging PDF documents using TCPDF

How can I create a new document using other PDFs that I'm generating?
I have methods to create some documents, and I want to merge them all in a big PDF, how can I do that with TCPDF?
I do not want to use other libs.
TCPDF has a tcpdf_import class, added in 2011, but it is still "under development". If you don't want to use anything outside of TCPDF, you're out of luck!
But FPDI is an excellent addition to TCPDF: it's like an addon. It's as simple as this:
require_once('tcpdf/tcpdf.php');
require_once('fpdi/fpdi.php'); // the addon
// FPDI extends the TCPDF class, so you keep all TCPDF functionality
$pdf = new FPDI();
$pdf->setSourceFile("document.pdf"); // must be pdf version 1.4 or below
// FPDI's importPage returns an object that you can insert with TCPDF's useTemplate
$pdf->useTemplate($pdf->importPage(1));
Done!
See also this question:
TCPDF and FPDI with multiple pages
Why don't you use Zend_PDF, it 's really a very good way to merge file.
<?php
require_once 'Zend/Pdf.php';
$pdf1 = Zend_Pdf::load("1.pdf");
$pdf2 = Zend_Pdf::load("2.pdf");
foreach ($pdf2->pages as $page){
$pdf1->pages[] = $page;
}
$pdf1->save('3.pdf');
?>
Hi i think TCPDF is not able to merge pdf files.
You can try it with an shell command and
PDFTK Toolkit
So you dont have to use an other pdf library.
This thread is from 2009, but using existing PDFs in PHP is still an issue in 2020.
After Zend_PDF has been abandoned and TCPDI does not support PHP 7, FPDI currently seems one of the few working solutions left in 2020. It can be used with TCPDF and FPDF, so existing code keeps working. And it currently seems well maintained.
Check out FPDI and FPDF_TPL. This isn't a perfect solution, but you can basically use FPDF_TPL to create a template of your PDF file and the insert it into your PDF file.

Categories