<p>You'd need to make a simple Craft <a href="http://buildwithcraft.com/docs/plugins/introduction" rel="nofollow">plugin</a> that added a Twig <a href="http://twig.sensiolabs.org/documentation" rel="nofollow">filter/function</a> that wrapped PHP's <a href="https://secure.php.net/manual/en/function.file-get-contents.php" rel="nofollow">file_get_contents</a> to grab the file contents, but at that point, you might as well just make it a wrapper for <a href="http://buildwithcraft.com/classreference/services/HttpRequestService#sendFile-detail" rel="nofollow">craft()->request->sendFile()</a>.</p>

CC BY-SA 3.0
2015-11-30T20:33:22.770Z
Thx for the keywords and direction!! https://gist.github.com/samhernandez/352901974c99207fa81e
emc
CC BY-SA 3.0
2015-11-30T20:03:37.993Z
2015-11-30T20:03:37.993Z
Brad Bell
<p>In March, I released a <strong>Digital Download</strong> plugin which makes this process extremely simple...</p>

<pre class="lang-html prettyprint-override"><code>&lt;a href="{{ craft.digitalDownload.url(asset) }}"&gt;{{ asset.title }}&lt;/a&gt;
</code></pre>

<p>View the <a href="https://craftpl.us/plugins/digital-download" rel="nofollow">complete documentation</a> to read more about how it works.</p>

CC BY-SA 3.0
2016-07-17T16:36:20.180Z
Thanks for contributing to the community! I definitely spent more than $30 worth of time trying to figure this out :D
emc
CC BY-SA 3.0
2016-07-17T06:07:17.370Z
2016-07-17T06:07:17.370Z
Lindsey D
<p>Is it possible to serve a file in a Twig template? I need to get a file (asset) from a global setting, and serve it at a particular URL.</p>

<p>This code is close, but it seems that <code>source()</code> is the wrong method to get the file contents with.</p>

<pre><code>{% header "Content-type: application/pdf" %}
{% header "Content-Disposition: inline" %}

{% set filePath = [craft.config.environmentVariables.assetPath, filesImages.deliveryCalendar[0].filename]|join('') %}

{{ source( filePath ) }}
</code></pre>

<p>The variable <code>filePath</code> contains the correct system path to the file (/var/www/site.com/html/assets/file.pdf), but <code>source()</code> is clearly for templates and not files.</p>

<p>How can the contents of a file be streamed out to the browser?</p>

CC BY-SA 3.0
2015-12-01T05:56:22.457Z
For those interested: Brad's answer is very valid but I couldn't get controller actions to work. Instead I added a Twig extension modelled very closely after [dbug()](https://github.com/lukeholder/craft-dbug/blob/master/dbug/extensions/DbugTwigExtension.php), then used the above template file with my custom extension `{{ dlfile(cal.pdfFile[0].id) }}` to dump the contents of the PDF. https://gist.github.com/emcniece/bcf3d902e5469613fb1c
emc
<templating><assets><filesystem><pdf>
Serve PDF file at url with variable
# Serve PDF file at url with variable

(Asked by: username_0 on 2015-11-30)

Is it possible to serve a file in a Twig template? I need to get a file (asset) from a global setting, and serve it at a particular URL.
This code is close, but it seems that source() is the wrong method to get the file contents with.
{% header "Content-type: application/pdf" %}
{% header "Content-Disposition: inline" %}

{% set filePath = [craft.config.environmentVariables.assetPath, filesImages.deliveryCalendar[0].filename]|join('') %}

{{ source( filePath ) }}

The variable filePath contains the correct system path to the file (/var/www/site.com/html/assets/file.pdf), but source() is clearly for templates and not files.
How can the contents of a file be streamed out to the browser?

**Tags:** templating, assets, filesystem, pdf
> **Comments:**
> **username_0**: For those interested: Brad's answer is very valid but I couldn't get controller actions to work. Instead I added a Twig extension modelled very closely after [dbug()](https://github.com/lukeholder/craft-dbug/blob/master/dbug/extensions/DbugTwigExtension.php), then used the above template file with my custom extension `{{ dlfile(cal.pdfFile[0].id) }}` to dump the contents of the PDF. https://gist.github.com/emcniece/bcf3d902e5469613fb1c  

---

## 2 answers

### Answer by username_1 on 2015-11-30. Score: 3 | Accepted answer

You'd need to make a simple Craft [plugin](http://buildwithcraft.com/docs/plugins/introduction) that added a Twig [filter/function](http://twig.sensiolabs.org/documentation) that wrapped PHP's [file\_get\_contents](https://secure.php.net/manual/en/function.file-get-contents.php) to grab the file contents, but at that point, you might as well just make it a wrapper for [craft()->request->sendFile()](http://buildwithcraft.com/classreference/services/HttpRequestService#sendFile-detail).

> **Comments:**
> **username_0**: Thx for the keywords and direction!! https://gist.github.com/samhernandez/352901974c99207fa81e  

---

### Answer by username_2 on 2016-07-17. Score: 2

In March, I released a Digital Download plugin which makes this process extremely simple...
{{ asset.title }}

View the complete documentation to read more about how it works.

> **Comments:**
> **username_0**: Thanks for contributing to the community! I definitely spent more than $30 worth of time trying to figure this out :D  

---

