Save pdf to file using template

I can create and save a pdf file with:

pdf = PDF::Writer.new()
pdf.select_font “Times-Roman”
pdf.text “Sample PDF Document”, :font_size => 32, :justification =>
:center
pdf.save_as(“pdffilename.pdf”)

but I’d like to re-use rpdf template, i I use the following code:

pdf_content = render_to_string(:template =>
“documents/#{template}-#{kind}”, :layout => false)
set_no_cache_headers
send_data(pdf_content, :filename => “#{@document.name_for_file}.pdf”,
:type => ‘application/pdf’, :status => 200)

I have the error “Can only render or redirect once per action”.
How can I save pdf to server disk without render twice ?

thanks
kind regards,
Enrico.