I was working on a laravel project on windows using xampp but switched to ubuntu after, my pdf generation code suddenly broke without any changes. I installed laravel-snappy and pointed it to the wkhtmltopdf paths but suddenly headers and footers are not getting rendered. This is my controller code
public function createPDF(){
$userData = $this->getUserData(); //query and returns all users and their relations
$pdf = App::make('snappy.pdf.wrapper');
$footer = \view('supporting.footer')->render();
$header = \view('supporting.header')->render();
$userData = \collect([$this->userData[1]]); //just for faster rendering and testing
$pdf->loadView('orders', ['users' => $userData])
->setOption('margin-top', '20mm')
->setOption('margin-bottom', '20mm')
->setOption('minimum-font-size', 25)
->setOption('header-html', $header)
->setOption('footer-html', $footer);
$pdf->save($this->path);
}
my header view:
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<div style="background: blanchedAlmond;color:green;">My Header is amazing<div>
</body>
</html>
This is my footer view:
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<div style="background: blanchedalmond; color:green;">Copyright © 2020</div>
</body>
</html>
My orders view: (just some basic tables)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Order Layout</title>
<style>
*{
font-family: cursive;
}
.wrapper {
display: block;
}
.invoice {
display: block;
width: 80%;
margin: 0 auto;
margin-top: 10px;
padding-top: 10px;
padding-bottom: 10px;
background: #d3d3d3;
page-break-inside: avoid !important;
padding-left: 20px;
}
.order-items {
padding-left: 100px;
}
.table {
width: 90%;
align-self: center;
border: 1px solid black;
page-break-inside: avoid !important;
orphans: 15;
}
.table>* {
text-align: center;
}
</style>
</head>
<body>
<main class="wrapper">
@foreach ($users as $user)
@php
$orders = $user->orders //just for renaming
@endphp
@foreach ($orders as $order)
<div class="invoice">
@php
$sum=0;
@endphp
<h2>{{$user->name}}: {{$order->id}}</h2>
<div class="order-items">
<table class="table" border="1">
<thead>
<tr>
<th>Product Name</th>
<th>Unit Price</th>
<th>Qty</th>
<th>subtotal</th>
</tr>
</thead>
<tbody>
@foreach ($order->products as $product)
<tr>
<th>
{{$product->name}}<br>
</th>
<td>{{$product->unit_price}}</td>
<td>{{$product->pivot->quantity}}</td>
@php
$sum+= $product->pivot->quantity*$product->unit_price
@endphp
<td>{{$product->pivot->quantity*$product->unit_price}}</td>
</tr>
@endforeach
</tbody>
<tfoot>
<tr>
<th colspan="3">Total:</th>
<td>{{$sum}}</td>
</tr>
</tfoot>
</table>
</div>
</div>
@endforeach
@endforeach
</main>
</body>
</html>
This is my apache config file:
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /home/frapto/frapto/pdf-mock/public
ErrorLog "/home/frapto/example.error.log"
CustomLog "/home/frapto/example.access.log" combined
<Directory "/home/frapto/frapto/pdf-mock/public">
Options -Indexes
DirectoryIndex index.php index.html
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
This is the pdf layout comparison is on: https://i.stack.imgur.com/DvMvT.jpg
No headers and footers getting generated, and table headers are not repeated when table is broken across pages
I have not changed anything in the code, I have no idea why it suddenly broke and how can i fix it? I tried multiple css properties, routes, and everything i could find and had no luck with any.
I'm open to switching libraries. Though my pdf may be large (currently 3000 pages roughly but may grow or shrink), dompdf simply doesnt work, tcpdf has unclear documentation, and mpdf is too slow. All I want to generate is are divs/tables (and their css) with header and footer on each page.
Thank you
EDIT:: it turned out I just needed to install the qt patched version of wkhtmltopdf library rather than the normal one