PDF/File Download Page(Template)

Hi,
I would like to create a page that automatically downloads a pdf-file as soon as I select this page via a link.

More precisely:
I would like to have a link to a page with a template in the footer-menu, which only offers a PDF for download instead of page content (preferably without actually calling up the this page).

Is this the correct or even best way to do this?

e.g.:

go($page->file('myfile.pdf')->url())

I would be very happy about your help/answers.

Best regards,
Robert

I don’t know if I understand what you want to achieve. Do you want to download a PDF when you click on a link? If so, this will work:

 <a href="<?= $page->file('myfile.pdf')->url() ?>" download>Download PDF</a>
1 Like

The alternative would be a route that returns $file->download()

Hallo and thank you for your answers.

@isaactopo:
I know about the download attribute, but unfortunately that’s not what I would like to do.
I would like to create a page (+ template + blueprint) where the editor in the backend sees only a field for the file-upload. If this page is called up by a visitor (click on the link in the [Footer] menu), the dialog box of the browser which offering the download should open instead of the page content.
I also know the way with pure PHP

header('Content-Type: application/pdf');
....
...
readfile('xyz.pdf')

I just thought that Kirby might already have a built-in-function/method that provides this, but as always, I’m missing what I’m looking for :frowning:

@texnixe
I would like to have it based on a temple and not with a route. A route refers more to a page, right?
As already described above, the editor should simply select the template (e.g. with the name “Download File”) when he creates a page, insert the file and save it. If the page is listed, it is displayed in the menu and when a website visitor click it, no page appears, but the download is offered…

Has anyone already done this and if so, how?

Thanks,
Robert

The you can just put the $file->download() code in the template.

1 Like

Thanks for your answer!

Then I’ll probably do it exactly as you instructed. I’m not quite sure yet whether forwarding with go() isn’t the more user-friendly way, since the PDF may then be displayed directl if the browser supports it.

Best regards,
Robert

You could also create a model for the download template, and overwrite the url function to point to the file instead. This way you don’t even need a template.

site/models/download.php

<?php

use Kirby\Cms\Page;

class DownloadPage extends Page {
    public function url($options = null): string {
        if($file = $this->file('myfile')) {
            return $file->url();
        }

        return parent::url($options);
    }
}

haven’t tried it, but should work :slight_smile:

That sounds really great at all. I will try it out. Thanks for the hint. :grinning: