I have the route /document/:email/:filename, :email => /.*/, :filename => /.*/
that simply takes the filename, searches for it on the storage and returns it. However, after I started using wicked_pdf for other component, the param[:filename]
in my controller stopped recognizing the .pdf extension. So before wicked_pdf the route /document/somemail@mail.com/myfile.pdf
generated the param param[:filename] == 'myfile.pdf'
on my controller, but after I integrated wicked_pdf the param is without the file extension i.e. param[:filename] == 'myfile'
how can avoid such behavior?
I don't want wicked_pdf to handle all the pdf files request of my application, only for a specific route/controller

- 320
- 4
- 14
2 Answers
I believe this is because in the Wicked PDF Railtie, it registers the extension like this:
if Mime::Type.lookup_by_extension(:pdf).nil?
Mime::Type.register('application/pdf', :pdf)
end
So before, :filename
was just a route parameter, but now that Rails knows there's a matching extension, it seems that it is treating it as a filename that can be followed by an extension, which should be available as params[:format]
.
You should be able to get the full filename by referring to it as
filename = [params[:filename], params[:format].compact.join('.')
Or unregister the Mime extension like this (maybe in the wicked_pdf initializer):
Mime::Type.unregister(:pdf)
There may also be a way to modify your route globbing to include the extension as part of the filename, but other StackOverflow threads related to that topic seem that it may not be possible to do that without trouble with filenames that also contain periods in them:

- 18,485
- 7
- 55
- 78
-
Hey thanks, but I couldn't manage to do either of your solutions. The get request was made with a URL so I couldn't set the appropriate mime type header request. Unregistering the :pdf didn't work neither because the middleware was modifying the request. Take a look at my solution that just posted – Paulo Madroñero Jun 13 '20 at 16:02
-
Found a blog post about how to allow dots in routes here: https://prathamesh.tech/2020/06/15/allowing-dots-in-rails-routes/ TL;DR: Specify your param with a more accepting regex like `post '/document/:id/:filename', filename: /[^\/]+/`. – Unixmonkey Jun 18 '20 at 16:02
So I discovered that the pdf extension is removed in the middleware, but I also discovered that the middleware can get some conditions so we can tell when to process the file with wicked_pdf and when not to. See https://github.com/mileszs/wicked_pdf/blob/3e0e2e7bd131365769d230c23ea17b4e52d2702f/lib/wicked_pdf/middleware.rb#L65
Then on my application.rb
I just put that condition:
wickedpdfFormat = /wickedpdf_/
config.middleware.use(WickedPdf::Middleware, {}, {:only => [wickedpdfFormat]})
And now I just have to include the wickedpdf_ prefix only for the files I want to be handled by wicked_pdf

- 320
- 4
- 14