It seems like the issue you're facing is related to custom fonts not being rendered correctly when deploying your Laravel application on Forge and generating PDFs using Spatie's laravel-pdf package, which internally uses Browsershot and Puppeteer.
Here are some steps you can take to ensure that custom fonts are rendered correctly in your PDFs:
-
Ensure Fonts Are Accessible: Make sure that the URLs provided for the font files are publicly accessible and that there are no CORS issues that might prevent the fonts from being loaded by Puppeteer.
-
Install Fonts on the Server: Since Puppeteer uses the Chrome browser to render the PDF, it will need access to the fonts installed on the system. You can install the custom fonts directly on your Forge server.
To install custom fonts on your Forge server, you can use the following steps:
- Connect to your server via SSH.
- Create a directory for custom fonts if it doesn't exist:
sudo mkdir -p /usr/share/fonts/custom - Upload your font files to this directory.
- Update the font cache:
sudo fc-cache -fv
-
Use Absolute Paths for Fonts in CSS: When specifying the
srcfor@font-facein your CSS, use absolute file paths instead of URLs. This ensures that Puppeteer can access the fonts without needing to make network requests.For example:
@font-face { font-family: "HelveticaNeueLTStd-Lt"; src: local('HelveticaNeueLTStd-Lt'), url(file:///usr/share/fonts/custom/HelveticaNeueLTStd-Lt.woff2) format('woff2'); } -
Adjust Browsershot Settings: You may need to adjust the Browsershot settings to ensure that it waits for all resources to load before taking the screenshot (generating the PDF). You can use the
waitUntilmethod to wait for thenetworkidle0ornetworkidle2event, which indicates that there are no more network connections for at least 500 ms.$browsershot->waitUntil('networkidle0'); -
Check Permissions: Ensure that the directory where the fonts are stored and the font files themselves have the correct permissions so that the web server and Puppeteer can read them.
-
Debugging: If the issue persists, you can try to debug by taking a screenshot of the page before generating the PDF to see if the fonts are rendered correctly in the screenshot.
$browsershot->screenshot('debug.png');
Here's an updated version of your controller method with the suggested Browsershot settings:
public function __invoke()
{
$pdf = PDF::view('pdf.invoice', [
'invoiceNumber' => '1234',
'customerName' => 'Grumpy Cat',
])
->withBrowsershot(function (Browsershot $browsershot) {
$browsershot->newHeadless();
$browsershot->deviceScaleFactor(3);
$browsershot->waitUntil('networkidle0'); // Wait for network to be idle
})
->orientation('portrait')
->paperSize(8.5, 11, 'in');
// Uncomment the line below to debug by taking a screenshot
// $browsershot->screenshot('debug.png');
$pdf->save('test.pdf');
return $pdf->download('invoice.pdf');
}
Remember to replace the url(file:///usr/share/fonts/custom/HelveticaNeueLTStd-Lt.woff2) with the correct path to your font file on the server.
By following these steps, you should be able to get custom fonts working in your PDFs on your Forge deployed app. If you continue to have issues, you may need to look into the server logs or Browsershot/Puppeteer logs for more detailed error information.