Laravel 5.0 - Generate PDF from template and add data - php

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.

Related

Create PDF Laravel 5.5?

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.

Generate fillable / editable PDF in Laravel

I am generating DOM PDF in laravel using barryvdh/laravel-dompdf package. It is working fine by rendering the views and HTML elements with readonly mode. But, I am trying to generate fillable / editable PDF so that user can enter details with out opening it in 3rd party editor tools.
Below is the code snippet I am using to generate PDF with barryvdh/laravel-dompdf package.
$file = "Storage/pdf/document.pdf";
$data = array("foo" => "bar");
$view = view('pdf.document', $data);
\PDF::loadHTML($view)->setPaper('A4', 'portrait')->setWarnings(false)-save($file);
I have also tried mpdf niklasravnsborg/laravel-pdf package but this is also opening in readable mode.
$data = [
'foo' => 'bar'
];
$pdf = PDF::loadView('pdf.document', $data);
return $pdf->stream('document.pdf');
Please suggest me if I need to configure any options to this code.
Create a PDF with a fillable form with dompdf its not possible, because it's not supported, owner says:
Dompdf supports rendering form fields as static content in the PDF but
not for rendering fillable forms.
Using niklasravnsborg/laravel-pdf that uses mpdf, you can use active forms to archive a fillable PDF.
There's an extense mpdf active forms example.
So, the blade html should look like:
<h2>Active Forms</h2>
<form action="formsubmit.php" method="post">
<b>Input Text</b>
<input type="text" size="90" name="inputfield" value="" title="The title attribute works like a tool-tip" />
</form>
Another possible solution would be use laravel-fpdf package, that uses FPDF.
You could also use FPDF. I think it's the best open-source PDF generator there is. You can easy import it, and you can select one dozen different pdf templates.

How to print only form.blade.php in Laravel dompdf

I want to export/print only a form in PDF.
This is the entire form
and I want to print just the those inside the box.
the main form is app.blade.php and it includes the side panel,header and the mid content(the data inside the red "box")
this is the code I've done so far:
public function exportToPDF($projectID){
$project = Project::find($projectID);
$users = $project->users;
$pdf = PDF::loadView('admin.projects.more', compact('project','users'));
return $pdf->download('invoice.pdf');
}
it shows no error and downloads the file, but it is like this:
I don't understand why it even gets all the other stuff, when I've declared only projects.more
any idea?
Use Inline CSS ,I have same issue Dompdf doesn't support external css

cakephp 3 and fpdf cannot render pdf in form of a view

**Controller** : Test
**View where the form resides** : index.ctp
**View containing fpdf code** : pdf.ctp
I generate a pdf file using FPDF but the pdf file is not rendering properly. I have confirmed that the FPDF code is error free.
On URL
localhost/project/test
I will fill up a form and press submit. This will submit to
localhost/project/test/pdf
But here the pdf is not rendering properly. It just keeps on telling Loading.
If i submit it to some other folder/pdf.php file (same fpdf code in php file)
Everything is ok
localhost/fpdf/pdf.php
I'm answering my questions myself. Come on people HELP ME!!
The problem was that the output was not rendering as pdf. I used this controller to solve my problem.
public function pdf()
{
$this->response->type('pdf');
}

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.

Categories