# Renderer: rendering to \*another\* format inside a renderer

**URL:** <https://www.ruby-forum.com/t/renderer-rendering-to-another-format-inside-a-renderer/191059>\
**Category:** Rails\
**Created:** [July 3, 2010, 7:03pm UTC](https://www.ruby-forum.com/t/renderer-rendering-to-another-format-inside-a-renderer/191059 "2010-07-03T19:03:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [July 3, 2010, 7:03pm UTC](https://www.ruby-forum.com/t/renderer-rendering-to-another-format-inside-a-renderer/191059/1 "2010-07-03T19:03:58Z")

</div>

My skeletal renderer looks like this

ActionController.add\_renderer :pdf do |template, options|  
html = render\_to\_string(template, options)  
…  
end

The trouble here is that render\_to\_string looks for a template for MIME  
type PDF, e.g. show.pdf.erb. Unfortunately, that’s not what I want. I  
need to get HTML. I haven’t found a straightforward option to achieve  
this.

html = render\_to\_string(options.merge(  
:template =\> “#{template}.html”)

That’s not pretty, but gets me halfway there. However, when I call

render :pdf =\> ‘show’, :layout =\> ‘print’

the layout is passed through to render\_to\_string, but ignored. If I  
rename layouts/print.html.erb to layouts/print.pdf.erb, it works as long  
as all partials used by that layout are available in pdf variants, too.

Now, I see that this behavior might be useful most of the time, but this  
time it is exactly not what I want. Is there a _clean_ way to make  
render\_to\_string do what I want?

Michael

–  
Michael S.  
[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.schuerig.de/michael/](http://www.schuerig.de/michael/)

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [July 3, 2010, 8:44pm UTC](https://www.ruby-forum.com/t/renderer-rendering-to-another-format-inside-a-renderer/191059/2 "2010-07-03T20:44:26Z")

</div>

Michael S. wrote:

> My skeletal renderer looks like this
> 
> ActionController.add\_renderer :pdf do |template, options|  
> html = render\_to\_string(template, options)  
> …  
> end
> 
> The trouble here is that render\_to\_string looks for a template for MIME  
> type PDF, e.g. show.pdf.erb. Unfortunately, that’s not what I want. I  
> need to get HTML.

WTF? What problem are you trying to solve that means you need to serve  
HTML when a PDF file is requested? I don’t see how that can ever be the  
right thing to do.

–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

Sent from my iPhone

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [July 3, 2010, 10:20pm UTC](https://www.ruby-forum.com/t/renderer-rendering-to-another-format-inside-a-renderer/191059/3 "2010-07-03T22:20:36Z")

</div>

On Saturday 03 July 2010, Marnen Laibow-Koser wrote:

> > The trouble here is that render\_to\_string looks for a template for  
> > MIME type PDF, e.g. show.pdf.erb. Unfortunately, that’s not what I  
> > want. I need to get HTML.
> 
> WTF? What problem are you trying to solve that means you need to  
> serve HTML when a PDF file is requested? I don’t see how that can  
> ever be the right thing to do.

I’m passing the HTML into an HTML-to-PDF converter. wkhtmltopdf in my  
case. wicked\_pdf does the same, but it works by alias\_chain-ing render,  
which I’d like to avoid. Instead, I’m defining a renderer. See

[http://www.engineyard.com/blog/2010/render-options-in-rails-3/](http://www.engineyard.com/blog/2010/render-options-in-rails-3/)

Michael

–  
Michael S.  
[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.schuerig.de/michael/](http://www.schuerig.de/michael/)
