레일스 애플리케이션에서 맞춤식 PDF를 다운로드 할 수있는 특별한 경로가 필요합니다.PDFKit과 Rails가 포함 된 ERB 템플릿의 PDF
이 PDF는 내 애플리케이션의 ERB 템플릿에서 PDFKit을 통해 생성해야합니다. 대신에 내가 달성하고 싶은 무엇을 설명, 나는 더 약간의 비 실행하지만 주석 코드를 붙여 넣습니다
class MyController < ApplicationController
def download_my_list_as_pdf
# The template uses the instance variables below
@user_id = params[:user_id]
@items = ['first_item', 'second_item']
# This line describes what I'd like to do but behaves not like I want ;)
# Render the ERB template and save the rendered html in a variable
# I'd also use another layout
rendered_html = render :download_my_list_as_pdf
kit = PDFKit.new(rendered_html, page_size: 'A4')
kit.to_pdf
pdf_file_path = "#{Rails.root}/public/my_list.pdf"
kit.to_file(pdf_file_path)
send_file pdf_file_path, type: 'application/pdf'
# This is the message I'd like to show at the end
# But using 'render' more than once is not allowed
render plain: 'Download complete'
end
end
내가 아직이 문제에 대한 답을 찾을 수 없습니다, 어떤 도움을 크게 감상 할 수있다!
안녕하세요, @max, 귀하의 답변과 추가 설명해 주셔서 감사합니다! 당신이 [당신이 찾고있는 것을 안다] (http://guides.rubyonrails.org/layouts_and_rendering.html#using-render);) 할 때 무언가를 찾기가 쉽다는 것을 다시 한번 보여줍니다. 이제 이것을 사용했습니다 :'rendered_html = render_to_string : download_my_list_as_pdf, layout : false' – krebas