Create PDF Laravel 5.5? - php

I want to create a pdf page from html file in Laravel but I do not know how to do it. You can help me
https://github.com/barryvdh/laravel-dompdf

// Import in top of controller file
use Barryvdh\DomPDF\Facade as PDF;
// use below code to generate and download pdf file
$pdf = PDF::loadView('bladefilename', ['arrayname' => $array]);
return $pdf->download('filename.pdf');
Write Above code in the controller file
In Blade file (HTML file) set array data.
Show Below Link : https://github.com/barryvdh/laravel-dompdf/issues/126

I know I am a little bit late but hopefully this will help someone.
There are 2 ways:
Create HTML page and use jspdf to print this page. The best way is to use html2pdf: https://github.com/eKoopmans/html2pdf.js
You can use this one to convert html to canvas and print to pdf. All css will be kept but you have to change the page to fix the space between pages as there may be some bad cut at the end of each page.
Print the page using latex. You have to use laravel latex compiler: https://github.com/fvhockney/latexcompiler
It is a little bit harder to styling the page but there is no problem with paging.

Related

Laravel 5.0 - Generate PDF from template and add data

This is the case:
I have a form where the user has to fill in 5 fields. I also have a PDF template with some layout with 5 empty colored boxes.
Now when the user fills in the form a PDF should be generated from the pdf template and the fields that the user has given in should be added to the template.
Now my question what's the best and easiest way to do this? I've look at the library laravel-dompdf but it didn't look like it's working with templates.
Did anybody had to do this? If yes, how did you do it? I'm working with laravel 5.0 .
I've used the packages fpdf and fpdi to work with pdf templates.
I use the "vsmoraes/laravel-pdf": "^1.0" package and it does well for us.
use Vsmoraes\Pdf\Pdf;
private $pdf;
public function __construct(Pdf $pdf)
{
$this->middleware('auth');
$this->pdf = $pdf;
}
public function pdfMaker($data)
{
$html = view('pdf.shippinglabel')->with('purchase', $purchase)->with('products', $products)->render();
$label = $this->pdf->load($html)->output();
file_put_contents(storage_path()."/ftp/po/label_".$id.".pdf",$label);
}
And then I have the formatted pdf with embedded CSS and not including external CSS file linking.
here's the github url: https://github.com/vsmoraes/pdf-laravel5
Dompdf doesn't work with pdf templates, but it works with HTML templates.
So, you can do this way:
Prepare a HTML template and place some placeholders( i.e: {field1}, {field2}, ecc ) where you want to print the values you'll get from the form
When the form is submitted, load the html template into a string, replace the placeholders in the string with the actual values you got from the form
Pass the string (that will contain valid html) to dompdf and it will generate a PDF file
With the Laravel package from Barry (https://github.com/barryvdh/laravel-dompdf) you can load views. So in that way it is possible to make a template using the Blade views.

PDF Generation Not keeping StyleSheet

So I am using the codeigniter pdf library from: https://github.com/chrisnharvey/CodeIgniter-PDF-Generator-Library
And it works wonders except, It doesn't keep the style sheet when the pdf is generated.
I am trying to use bootstrap to make it look nice, But when I run the script and download the pdf it doesn't have the styling anymore. What do I need to do to keep the stylesheet linked?
My Controller:
public function AdminPracticeSheetLateReport()
{
$this->load->view('pdf/practiceLateReport');
$this->pdf->load_view('pdf/practiceLateReport');
$this->pdf->render();
$this->pdf->stream("welcome.pdf");
}
I am loading the bootstrap stylesheet like so in my view:
<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
Is this even possible with this plugin? If not it's quite the crappy plugin if you ask me.
BTW: This library for codeigniter uses the DomPDF
I just checked the source code of codeigniter and noticed that it does not itself make a PDF, but uses DOMPDF in turn (which is another PHP library). codeigniter does not properly send the html and css to DOMPDF, so in my suggestion you should remove codeigniter and use DOMPDF instead. This should correct your problem and also speed up the conversion by a few miliseconds.

how to convert dynamic contents of html page to pdf

In html page some tags are dynamically created using jquery and contents are loaded from msql db using jquery and php.
I want convert this dynamic page to pdf.
I have tried following code but it generate pdf of static part of html page.
<?php
ob_start();
?>
//html code and internal css, javascript used in external file with jquery library
<?php
include('dompdf/dompdf_config.inc.php');
$contents = ob_get_contents();
ob_end_clean();
$dompdf = new DOMPDF();
$dompdf->load_html($contents);
$dompdf->render();
$dompdf->stream('file.pdf');
?>
So how to store contents of dynamic html page in a php variable after processing it ( using javascript/php ) and convert it to pdf usin dompdf or other converter.
I'd suggest you take a look at wkhtmltopdf. I've had good results with getting it to render google charts, which are built dynamically from a javascript api.
http://code.google.com/p/wkhtmltopdf/
As Marc said, you have to read generated DOM with javascript.. something like $('html').html() and then post it to php to generate pdf
This may not meet exactly what you are looking for, but wkhtmltopdf could be what you need. Since PHP is a server-side technology, you will have a difficult time getting it to process any client-side javascript. wkhtmltopdf scans the page, javascript and all, then generates a pdf file on the server. Hope this helps you out!
Consider using a tool like wkhtmltopdf to directly generate a page as a PDF. It will run javascript and generate the page as WebKit would render it.

add text over image in fpdf

is there a way to add text with certain parameters over images in pdf?
can this text be for example
$pdf->Cell(0,260,''.$person["CA"],'C',0 ,1);
this: .$person["CA"] i called from a global in the top of the php file.. but would like to know if its possible too. ..??
any ideas???
There's no such built-in function in FPDF, but that doesn't mean you can't write your own routine to automate the process. Libraries like FPDF are designed to provide base functionality for you to extend.

Convert text to PDF in PHP

What would be the simplest, shortest way to turn a text file into a PDF file with PHP? With some basic example code if possible.
I've seen this but the examples don't show how to use a text file as input.
http://uk3.php.net/manual/en/book.pdf.php
Thanks
TCPDF and FPDF can both render PDF output. If you're on a Linux system, you could also call the system's ghostscript to do it.
You'll definitely need a library to write PDFs. I'd say try FPDF.
TCPDF is the best way to convert text or HTML to PDF.
http://www.tecnick.com/public/code/cp_dpage.php?aiocp_dp=tcpdf
Throwing Zend_Pdf into the ring as well.
And a somewhat old tutorial to get you started in addition to the extensive docs in the ZF manual:
http://devzone.zend.com/article/2525
You can use MPDF.
I wrote a smalll instruction:
1) download Full version(http://www.mpdf1.com/mpdf/index.php?page=Download ) and extract inside anyfolder
2) create sample.php in anyfolder and insert like this:
<?php
include('./mpdf.php');
$mpdf=new mPDF();
$mpdf->WriteHTML('<div style="color:yellow;">SAMPLE TEXT, WITH HTML TAGS Too!</div>');
$mpdf->Output(); die('');
?>
3) then open sample.php in your browser to see the result!
MANY OTHER ONES - Convert HTML + CSS to PDF with PHP?

Categories