laravel 5 html to pdf view issue - php

I am trying to put html and save it as pdf. however, it seems the content inside pdf not rendered well. I am using plugin from maatwebsite. Here is the code I've tried
Controller
Excel::create('test',function($file){
$file->sheet('Sheet1', function ($sheet){
$sheet->loadView('excel');
$sheet->setAutoSize(true);
});
})->store('pdf',storage_path('uploaded-excel'));
View
<body>
<p>test</p>
<img src="images/hierarchy.png"/>
</body>
Result
But when I remove img tag, the size itself looks normal
What am I missing?

I use https://github.com/barryvdh/laravel-dompdf plugin instead of using inbuilt function from maatwebsite

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.

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

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.

Using dynamic php generated images with MPDF

I am using php and mpdf to generate a particular pdf. I have some pie diagrams in the pdf. For that I am using pChart. I am generating pie diagrams dynamically for rendering in pdf.
In my pdf all the static images are rendered properly, but no dynamically generated pie diagrams.
This one(static) works,
<div class="chart">
<?php echo CHtml::image(Yii::app()->baseUrl.'/images/color-block2.png', 'Logo screenshot', array('class' => 'logo_image')); ?>
</div>
But not this(dynamic)
<div class="chart">
<?php echo CHtml::image(Yii::app()->baseUrl.'/images/pie.png', 'Logo screenshot', array('class' => 'logo_image')); ?>
</div>
Ps: I am using Yii url rules for a clean url for pie diagram.
When i rendered the pdf_template as html, both images are rendered properly.
How can i implement dynamic images using mpdf without the $mpdf->Image() method.
I suspect i cannot use the $mpdf->Image() while generating a pdf from html.
Update:
After
$mPDF->showImageErrors = true;
I'm getting
mPDF error: IMAGE Error (http://csrt.dev/images/pie.png): Could not find image file
Base64 encoded the image and embedded it in a <img> tag.
<div class="chart">
<img src="data:image/png;base64,xx5t.." />
</div>
I had the same problem using Yii, and found this to be the solution, however for other Yii/PHP users out there, you have to use base64_encode(your file contents) to get the part after base64, in the tag.
for example:
base64_encode(file_get_contents("../images/folder16.gif"));
I've had a similar issue.
Using:
<img src="http://domain.com/assets/img/header.png">
or
<img src="/assets/img/header.png">
give me the following error:
mPDF error: IMAGE Error (http://domain.com/assets/img/header.png): Could not find image file
Even though i can browse to that and see the image ok.
However using:
<img src="assets/img/header.png">
Seems to work fine.

Categories