我将简短介绍。我一直在尝试教自己如何在RubyonRails中使用PDFKit的一些基础知识。这个网络链接是我所遵循的所有步骤。
http://blog.clecotech.com/2014/08/download-pdfs-in-rails-using-pdfkit-gem.html
我准确地遵循了所有内容,并安装了所有必需的gem。不幸的是,每当我创建"link_to"时,localhost都不会显示页面。它一直用红色字体给我这个错误消息,上面写着:"很抱歉。出了问题。"但它没有显示错误的位置。
我建议你使用pdfgen gem,这个gem使用了puppeteer和headless chrome under hood,在我看来这是生成pdf最快的方法。
temp_file = Tempfile.new("test.pdf")
html = ApplicationController.render('pdf_view', layout: false, locals: {})
temp_file << Pdfgen.new(html).launch_options(args: ['--no sandbox']).to_pdf(printBackground: true)
temp_file.close
你只需将你的视图渲染成pdf,然后准备好文件