Hey! I’m working on a multi-language site (English & Arabic) and I’ve set up a system for generating and downloading PDFs from the page content. It works fine until I switch to the Arabic site, as the PDF is always generated with English content and styling (ltr). How do I account for the current site language when generating the PDF?
This is the builder in my config:
[
'pattern' => 'pdf/(.*)',
'action' => function ($fullSlug) {
$page = page($fullSlug);
if (!$page) {
return response::notFound('Page not found');
}
$currentLanguage = kirby()->language()->code();
$isArabic = ($currentLanguage === 'ar');
$dompdf = new Dompdf();
$options = $dompdf->getOptions();
$options->set('isRemoteEnabled', true);
$options->set('isHtml5ParserEnabled', true);
$options->set('defaultFont', 'Kaff');
$options->set('pdfBackend', 'CPDF');
$fontCachePath = kirby()->roots()->index() . '/assets/css/fonts/';
$options->setChroot([$fontCachePath]);
$options->setFontCache($fontCachePath);
$title = $page->title()->isNotEmpty() ? $page->title()->value() : '';
$date = $page->date()->isNotEmpty() ? $page->date()->value() : '';
$author = $page->author()->isNotEmpty() ? $page->author()->value() : '';
$logoUrl = url('assets/TLC-logo-01.png');
$content = $page->blocks()->toBlocks()->collectFootnotes();
$footnotes = Footnotes::footnotes();
$dir = $isArabic ? 'rtl' : 'ltr';
$lang = $currentLanguage;
$css = '
body {
font-family: Kaff;
direction: ' . $dir . ';
}
html[dir="rtl"] .container {
text-align: right;
}
a {
color: #A370FF;
text-decoration: none;
}
h1, h2, h3, h4, h5 {
font-family: Kaff-Bold;
}
.logo {
width: 120px;
margin-bottom: 30px;
}
.header {
text-align: center;
margin-bottom: 0;
}
.line {
width: 100%;
height: 1px;
background-color: #A370FF;
margin-top: 30px;
margin-bottom: 10px;
}
img {
width: 100%;
}
.articleDetails {
color: #A370FF;
}
figcaption {
font-size: 0.8em;
color: #A370FF;
margin-top: 1em;
text-align: center;
}
.container {
padding-left: 70px;
padding-right: 70px;
}
sup {
position: relative;
top: -0.2em;
font-size: 0.75em;
vertical-align: unset;
}
.footnotes-container {
font-size: smaller;
margin-top: 20px;
}
';
$htmlContent = '
<html dir="' . $dir . '" lang="' . $lang . '">
<head>
<meta charset="UTF-8">
<style>' . $css . '</style>
</head>
<body>
<div class="container">
<div class="header">
<img class="logo" src="' . $logoUrl . '" alt="Logo" />
</div>
<h1>' . $title . '</h1>
<p class="articleDetails">' . $date . '</br>' . t('textBy') . ' ' . $author . '</p>
<div>' . $content . '</div>
<div class="line"></div>
<div class="footnotes-container">' . $footnotes . '</div>
</div>
</body>
</html>';
$dompdf->loadHtml($htmlContent);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream($page->slug() . ".pdf", ["Attachment" => true]);
exit;
}
]
And this is the download link:
<a href="<?= url('pdf/' . $page->uri()) ?>" class="pdfButton">
<?= svg('assets/scan-icon.svg') ?>
</a>