DOMPDF: Laravel failed to load CSS when downloading PDf - php

ViewBlade
<!doctype html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<style type="text/css" media="all">
.HVMC{font-family: "Herr Von Muellerhoff", cursive !important}
.MDHC{font-family: "Mr De Haviland", cursive !important;}
.ABC{font-family: "Alex Brush", cursive !important;}
.CCC{font-family: "Cedarville Cursive", cursive !important;}
.LBAC{font-family: "La Belle Aurore", cursive !important;}
.img-list {
background-size: contain;
background-position: center;
background-repeat: no-repeat;
position: relative;
width: 100%;
height: 1310px;
}
html,body
{
height: 100%;
margin: 0;
background-color:lightgray;
}
</style>
</head>
<div id="maindiv">
#foreach ($proposalfromTemplate as $proposal)
{!! $proposal->html !!}
#endforeach
<div>
</html>
Controller
PDF::setOptions(['isRemoteEnabled' => TRUE, 'enable_javascript' => TRUE]);
$pdf = PDF::loadView('pdfview');
return $pdf->download($pid . '.pdf');
When I return View It works fine, but when I return $pdf->download() it not including CSS, in Blade file
the given data is a text file which I get from storage so I am not able to add CSS on it
Thanks

You have to render your HTML first before load into DOMPDF
public function generateXYZ()
{
PDF::setOptions(['isRemoteEnabled' => TRUE, 'enable_javascript' => TRUE]);
$dompdf = new Dompdf();
$html = view('your.view.path')->render();
$dompdf->loadHtml($html);
$dompdf->render();
return $pdf->download('card.pdf');
}

Related

Dompdf rendering spaces incorrectly with custom font

I'm using Dompdf v2.0.1 to make a report. I'm using a custom font from Google Fonts (Josefin Sans) but the spaces are rendered incorrectly.
But when rendering the page as HTML, spaces are displayed correctly.
Below I'll paste the HTML and PHP code I'm using to generate the PDF.
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Josefin+Sans:wght#300;400;700&display=swap" rel="stylesheet">
<title>Relatório de atendimentos</title>
<style>
#page {
margin: 16px;
padding: 0;
}
body {
font-family: 'Josefin Sans', sans-serif;
font-size: .8em;
}
table {
width: 100%;
border-collapse: collapse;
border: none;
}
td, th {
padding: .3em;
}
td {
border: solid 1px #000;
font-weight: 300;
}
.title {
font-size: 1.2em;
color: #FFF;
text-align: center;
background-color: #363435;
border-color: #363435;
}
</style>
</head>
<body>
<table>
<thead>
<tr>
<th class="title">Relatório detalhado de tickets</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<b>GERADO POR: </b> sadsadsad
</td>
</tr>
</tbody>
</table>
</body>
</html>
<?php
declare(strict_types=1);
namespace App\Actions\Report;
use Dompdf\Dompdf;
class GenerateReport
{
public static function run(array $data): string
{
return GenerateReportString::get($data);
$pdf = new Dompdf([
'isRemoteEnabled' => true,
'isJavascriptEnabled' => false,
'dpi' => 200,
]);
$pdf->setPaper('A4', 'landscape');
$pdf->loadHtml(
GenerateReportString::get($data)
);
$pdf->render();
return $pdf->output();
}
}
I tried setting the charset to UTF-8 in the HTTP header (Content-Type: application/pdf; charset=UTF-8), but the result is the same.
This is due to a font subsetting bug in the underlying php-font-lib library, see issue 2445. Not all fonts are impacted, but for those that are you can temporarily work around the issue by disabling subsetting.
$dompdf = new Dompdf(["IsFontSubsettingEnabled" => false]);
Note: This isn't a great solution if the font is large since it will impact the overall PDF size.

WKHTMLTOPDF (knplabs - snappy) footer style import

I'm using wkhtmltopdf to generate my pdf files.
I have main page and footer page that I am rendering. They are in separate view.
Problem with printing it is that just font is loaded and other is not.
I did the main page the same way and all of the css is loaded correctly.
My view of footer.html.twig
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Statement Footer</title>
<link href="https://fonts.googleapis.com/css?family=Courier+Prime&display=swap" rel="stylesheet">
</head>
<style type="text/css">
html {
height: 0;
background-color: white;
white-space: nowrap;
font-family: 'Courier Prime', monospace;
}
hr {
display: block;
height: 1px;
border: 0;
border-top: 1px solid #ccc;
margin: 1em 0;
padding: 0;
}
#footer{
display:table-cell;
width:33.33vw;
text-align: center;
}
</style>
<body>
<hr/>
<div id="footer">
Hello!
</div>
</div>
</body>
</html>
And in my controller:
$options = [
'encoding' => 'UTF-8',
'footer-html' => $footerHtml = $this->renderView('#WebsiteTemplates/invoice/footer.html.twig')
];
$html = $this->renderView('#WebsiteTemplates/invoice/invoice.html.twig');
$filename = sprintf('test-%s.pdf', date('Y-m-d'));
return new Response(
$this->get('knp_snappy.pdf')->getOutputFromHtml($html, $options),
200,
[
'Content-Type' => 'application/pdf',
'Content-Disposition' => sprintf('attachment; filename="%s"', $filename),
]
);
Style tags are good, and I have no css files as assets that I have to include. All of them are directly in a twig.

adding css onto the pdf

I need a pdf to be generated with name being placed on top of an image at a particular place. I have written a working HTML code but mpdf is unable to load the stylesheets and the name is getting printed below the image.
Here is my code of php:
<?php
$html = file_get_contents('index12.html');
require_once __DIR__ . '/vendor/autoload.php';
$mpdf = new \Mpdf\Mpdf();
$stylesheet = file_get_contents('style.css');
$mpdf->WriteHTML($stylesheet, 1);
$mpdf->WriteHTML($html,2);
$mpdf->Output();
index12.html
<!-- <link rel="stylesheet" href="style.css"> -->
<div class="image">
<img src="yugmak.jpg" alt="" />
<h2>Kung Fu Panda</h2>
</div>
style.css
#font-face {
font-family: "Montez";
src: url("Montez.ttf");
}
.image {
position: relative;
width: 100%; /* for IE 6 */
}
h2 {
position: absolute;
font-family: "Montez";
font-size: 250px;
top: 2100px;
padding-left: 1050px;
width: 2000px;
}
Any way to make the css work in pdf would be great.

PHP HTML to PDF mikehaertl/phpwkhtmltopdf Page Size Issue

I'm currently running mikehaertl/phpwkhtmltopdf package to convert HTML to PDF. Using the demo data I appear to be having issues with the PDF rendering the correct page dimensions. I need the page to be filled with an A4 layout, but I'm getting a much smaller resolution output for some reason. I believe there is either a setting I'm not aware of or I'm missing something in the HTML/CSS?
The PHP I am running is:
$pdf = new mikehaertl\wkhtmlto\Pdf(
array(
'binary' => '/usr/local/bin/wkhtmltopdf',
'no-outline', // Make Chrome not complain
'margin-top' => 0,
'margin-right' => 0,
'margin-bottom' => 0,
'margin-left' => 0,
// Default page options
'disable-smart-shrinking'
)
);
$pdf->addPage($this->getOrderPrint($objectId));
// Save the PDF
$pdf->saveAs(dirname(__FILE__).'/test.pdf');
The getOrderPrint method is correctly returnin the below HTML:
<!DOCTYPE html>
<head>
<style type="text/css">
body {
margin: 0;
padding: 0;
width: 21cm;
height: 29.7cm;
}
/* Printable area */
#print-area {
position: relative;
top: 1cm;
left: 1cm;
width: 19cm;
height: 27.6cm;
font-size: 10px;
font-family: Arial;
}
#header {
height: 3cm;
background: #ccc;
}
#footer {
position: absolute;
bottom: 0;
width: 100%;
height: 3cm;
background: #ccc;
}
</style>
</head>
<body>
<div id="print-area">
<div id="header">
This is an example headernbmv.
</div>
<div id="content">
<h1>Demo</h1>
<p>This is example content</p>
</div>
<div id="footer">
This is an example footer.
</div>
</div>
</body>
</html>
Try to add 'page-size' => 'A4' to your wkhtmltopdf options.

Render a partial view with layout in Zend Framework 1.12

Similar questions have been asked before but unfortunately I am still not able to find a solution of my problem.
I want to render a view with its layout and store its response into a variable.
I have an action called pdf as shown below.
public function pdfAction(){
$ids = $this->getParam('id');
$entity = $this->getParam('entity');
$referer = $this->getRequest()->getServer('HTTP_REFERER');
if(empty($ids) || empty($entity)){
$this->_helper->redirector->gotoUrlAndExit($referer);
}
$grid = $this->_exportModel->getExportGrid($ids, $entity);
if(empty($grid->data[0])){
$this->_helper->messenger->error('No user was found for the given id. Please pass the correct parameters and try again.');
$this->_helper->redirector->gotoUrlAndExit($referer);
}
/* I would like to render the view here and get its response below. */
$html = '';
$pdf = new Om_PDF();
$pdf->writeHtmlToPDF($html);
}
This is the pdf view for the above action.
<?php echo $this->render('templates/grid/print-grid.phtml'); ?>
And here's the pdf layout
<?php echo $this->doctype(); ?>
<html moznomarginboxes mozdisallowselectionprint>
<link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" media="all"/>
<style type="text/css">
.container {
margin: 0 auto;
max-width: 1000px;
text-align: center;
margin-top: 30px;
}
#media print {
#page {
size: auto;
margin: 15mm 10mm 15mm 10mm;
}
body {
margin: 0px;
}
.page-break {
display: block;
page-break-before: always;
}
.print-view-table {
float: left;
width: 45% !important;
margin-right: 5%;
}
.print-view-table tr th, .print-view-table tr td {
width: 30%;
}
.print-view-table tr th {
background-color: #e0e0e0 !important;
border: 1px solid #ddd;
-webkit-print-color-adjust: exact;
}
}
</style>
<head>
<?php ?>
</head>
<body>
<div class="container">
<div class="row">
<?php echo $this->layout()->content; ?>
</div>
</div>
</body>
</html>
So far I have tried using render() and partial() methods but none of them worked.
Is there a way using which I can get the out put of the pdf view along with its layout inside the pdfAction() method?
You need to use :
$this->_helper->layout->setLayout('/path/to/your/pdf_layout_script');
in your pdfAction() and after that you can use render() or partial() methods, the corrects layout should be out put

Categories