Skip to content

[4.x]: DOMPDF not working on Craft Cloud #3983

@timkelty

Description

@timkelty

What happened?

Thanks to @craig-lt for the report

The download PDF functionality is throwing a 500 error.
It seems to be related to the temp path DomPDF is trying to use for (custom) fonts.

This workaround seems to work for now:

Event::on(
Pdfs::class,
Pdfs::EVENT_MODIFY_RENDER_OPTIONS,
function(PdfRenderOptionsEvent $event) {
// Use Craft's designated temporary path, which is writable on Craft Cloud
$cloudTempPath = Craft::$app->getPath()->getTempPath();
$dompdfTempDir = $cloudTempPath . DIRECTORY_SEPARATOR . 'dompdf_temp';
$dompdfFontCacheDir = $cloudTempPath . DIRECTORY_SEPARATOR . 'dompdf_font_cache';

try {
FileHelper::createDirectory($dompdfTempDir);
FileHelper::createDirectory($dompdfFontCacheDir);
} catch (\yii\base\ErrorException $e) {
Craft::error("Could not create Dompdf temp/cache directories: " . $e->getMessage(), __METHOD__);
return; // Stop further processing in this listener
}

$event->options->setTempDir($dompdfTempDir);
$event->options->setFontCache($dompdfFontCacheDir);
$event->options->setFontDir($dompdfFontCacheDir);

// Set other helpful Dompdf options using setters
$event->options->setIsRemoteEnabled(true);
$event->options->setDefaultFont('sans-serif');
}
);

Craft CMS version

4.14.10

Craft Commerce version

4.8.2

PHP version

8.1

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions