Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-30T20:33:22.770Z
Answers.Text: Thx for the keywords and direction!! https://gist.github.com/samhernandez/352901974c99207fa81e
Answers.UserDisplayName: emc
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-30T20:03:37.993Z
Answers.LastActivityDate: 2015-11-30T20:03:37.993Z
Answers.UserDisplayName: Brad Bell
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-17T16:36:20.180Z
Answers.Text: Thanks for contributing to the community! I definitely spent more than $30 worth of time trying to figure this out :D
Answers.UserDisplayName: emc
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-17T06:07:17.370Z
Answers.LastActivityDate: 2016-07-17T06:07:17.370Z
Answers.UserDisplayName: Lindsey D
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-12-01T05:56:22.457Z
Comments.Text: 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
Comments.UserDisplayName: emc
Tags: <templating><assets><filesystem><pdf>
Title: Serve PDF file at url with variable
ThreadText: # 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: username_1'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...
  
  ```html
  <a href="{{ craft.digitalDownload.url(asset) }}">{{ asset.title }}</a>
  
  ```
  
  View the [complete documentation](https://craftpl.us/plugins/digital-download) 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  
  
  ---
  
  