简体   繁体   English

如何从symfony的公共文件夹下载pdf文件

[英]how to download pdf file from public folder in symfony

I'm using dompdf to generate invoices from html template, every work perfectly and the invoices are saved in the database with absolute filepath and filename, when I access to public/invoices I found all my pdf file there, I want to make a link button to download or open in the browser the file.我正在使用 dompdf 从 html 模板生成发票,每个工作都完美,发票以绝对文件路径和文件名保存在数据库中,当我访问公共/发票时,我在那里找到了所有的 pdf 文件,我想建立一个链接按钮下载或在浏览器中打开文件。

I create a link that take the absolute file like that :我创建了一个链接,它采用这样的绝对文件:

{% for invoice in invoices %}
<tr>
  <td> {{ invoice.id }} </td>
  <td>{{ invoice.fileName }}</td>
  <td>{{ invoice.booking.user.name }}</td>
  <td>{{ invoice.booking.car.registrationNumber }}</td>
  <td>{{ invoice.booking.startDate | date('d-m-Y') }}</td>
  <td><a href="" class="btn btn-sm btn-outline-danger">Archiver</a></td>
  <td><a href="{{ asset('invoices/' ~ invoice.filePath) }}) }}" style="color: #DDDDDD" class="btn btn-sm btn-dark"><i style="padding-right: 10px" class="fa fa-download"></i>Télecharger</a>
  </td>
</tr>
{% endfor %}

I got this error :我收到此错误:

No route found for "GET /home/sahnoun/Downloads/SousseCar/public/invoices/aKRyaziz.pdf)%20%7D%7D" (from "http://127.0.0.1:8000/admin/invoice/all")找不到“GET /home/sahnoun/Downloads/SousseCar/public/invoices/aKRyaziz.pdf)%20%7D%7D”的路线(来自“http://127.0.0.1:8000/admin/invoice/all”)

you are closing curly brackets two times when trying to use the asset() function, which generates the additional ")}}" in your output (url encoded: ")%20%7D%7D", which you can see in the error message).您在尝试使用 asset() 函数时关闭了两次大括号,该函数会在您的输出中生成额外的“)}}”(网址编码:“)%20%7D%7D”,您可以在错误中看到信息)。 So naturally, this path does not exist.所以自然而然,这条路是不存在的。

...
// remove surplus '}})'
href="{{ asset('invoices/' ~ invoice.filePath) }}) }}"
...

Right:对:

href="{{ asset('invoices/' ~ invoice.filePath) }}"

And really you should not expose invoice information to public Internet - that is basically huge security and user information leak - please move that logic inside your controller where you check if request user has proper rights to that information and then just return eg.而你真的应该暴露发票信息,以公共互联网-这基本上是巨大的安全和用户信息泄露-请动这种逻辑控制器里面,你是否要求用户具有适当的权限这些信息,然后就返回如。 BinaryFileResponse from that controller.来自该控制器的BinaryFileResponse

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM