2

This works fine with devMode(true), but not when false.

I'm calling it as a craft variable {% do craft.mymodule.pdf(entry) %}

public function pdf(Entry $entry)
{
    $asset = $entry->asset->one();

    $fsPath = Craft::getAlias($asset->getVolume()->fs->path);
    $file = $fsPath . '/' . $asset->getPath();

    header('Accept-Ranges: bytes');
    header('Cache-Control: maxage=1');
    header('Content-Length: ' . $asset->size);
    header('Content-Transfer-Encoding: binary');
    header('Content-Type: ' . $asset->getMimeType());
    header('Content-disposition: inline; filename=' . basename($asset->filename));
    header('Pragma: public');

    readfile($file);
    exit;
}

Error message

enter image description here

BTW, nothing in the logs when it fails.

1 Answer 1

2

I solved my problem moving part of my code to twig.

So final code looks like:

twig

{% set asset = entry.asset.one() %}
{% set file = alias('@assets') ~ '/' ~ asset.getPath %}

{% header 'Accept-Ranges: bytes' %}
{% header 'Cache-Control: maxage=1' %}
{% header 'Content-Transfer-Encoding: binary' %}
{% header 'Content-Type: ' ~ asset.getMimeType %}
{% header 'Content-disposition: inline; filename=' ~ asset.filename %}
{% header 'Pragma: public' %}

{% do craft.mymodule.pdf(file) %}

craft variable

public function pdf(String $file): void
{
    readfile($file);
}
1
  • Huh... really strange.
    – Brad Bell
    Commented Jun 12, 2023 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.