I want to display only specific sections on my blade to pdf view . is it possible using DOMpdf ? pls advice
public function pdf()
{
$pdf = PDF::loadView('home');
return $pdf->stream();
}
why you don't create a new blade file just for your section, and you shouldn't use any kind of extending like
#extends('layout')
so you will just create the view template file which contain your section
for example if your home have that section
#section('topdf')
<div class="pdfsection">My PDF Goes Here</div>
#endsection
so you will create a new blade file for example with the name mypdf.blade.php
and let it contain the just the code
<div class="pdfsection">My PDF Goes Here</div>
then your function will be like that
public function pdf()
{
$pdf = PDF::loadView('mypdf');
return $pdf->stream();
}
You could use media types to define a style that applies only during PDF generation. For example, a class that sets the display to none on sections you don't want included in the render.
#media print {
.noprint { display: none; }
}
<div>
<p>This will render to the pdf</p>
</div>
<div class="noprint">
<p>This will not render to the pdf</p>
</div>
Which media type you specify depends on which one dompdf is configured to use. In a default install dompdf uses the "screen" media type. To change the media type that dompdf uses:
In 0.6.2 or earlier set the DOMPDF_DEFAULT_MEDIA_TYPE configuration constant to "screen".
In 0.7.0 or later call $dompdf->set_option('defaultMediaType', 'print'); after instantiating dompdf.
dompdf also recognizes the "dompdf" media type, if for some reason you have some styling you only want to use when rendering to PDF with dompdf. Since this is an unofficial media type other devices should ignore it.
Related
I've got this code to export some data into a pdf. And I would like to add css from an external css file (which is not mentionned in the html used)
/*********************************** Export PDF ****************************************************/
if($request->query->get('exportPDF')!= null){
// Configure Dompdf according to your needs
$pdfOptions = new Options();
$pdfOptions->set('defaultFont', 'Arial');
// Instantiate Dompdf with our options
$dompdf = new Dompdf($pdfOptions);
// Retrieve the HTML generated in our twig file
$html = $this->renderView('dashboard/user_table.html.twig', [
'users' => $users
]);
// Load HTML to Dompdf
$dompdf->loadHtml($html);
// (Optional) Setup the paper size and orientation 'portrait' or 'portrait'
$dompdf->setPaper('A3', 'landscape');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser (force download)
$dompdf->stream("exportUsers.pdf", [
"Attachment" => true
]);
}
The user_table.html is only a file with a <table>
who has some class from a css file loaded in an other template. That means for DomPDF the file who contains the css is unkown and, as a result I have a table with no css in my pdf.
I've tried to add the stylesheet in my html directly but the import isn't working like that neither. But I don't want to add it in the html anyway, the css is loaded is a more hight level template.
How to add external files (like bootstrap etc etc) from this structure ? I do not know if this is even possible. Thanks for the help ;)
the css file must be referenced in the HTML you give to DomPDF.
If you don't want to change your twig template, you can use a workaround like this :
$dompdf = new Dompdf();
$html = $this->renderView('dashboard/user_table.html.twig', [
'users' => $users
]);
$html .= '<link type="text/css" href="/absolute/path/to/pdf.css" rel="stylesheet" />';
$dompdf->loadHtml($html);
Note that adding a link tag to the body is not valid according to HTML specifications. With the current Dompdf version, it works but it may not work in future versions.
You can always use solutions like this Including a non-twig file from twig read external file contents right into templates attribute, so css will be rendered inline and will be compatible with mpdf.
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
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
I am using yii2 and want to convert my html view to pdf format, for that I have use
mPDF yii2 install .
In my controller action , I am doing this:
public function actionPdf(){
Yii::$app->response->format = 'pdf';
$this->layout = '//print';
return $this->render('myview', []);
}
When I remove Yii::$app->response->format = 'pdf'; this line from action. I always get my correct view with css.
But when I use Yii::$app->response->format = 'pdf'; this for pdf generation , I get only pdf view without css.
I have included my CSS file in layout. What should I have to do for getting my correct pdf view?
Have you included the CSS inline?
Is the CSS you expect allowed by mPDF?
Basically, yii2-pdf is a thin wrapper around the mPDF library. It will not include your styles if they are required through a <link, only if they are included as a <style tag.
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.