简体   繁体   English

如何使用API​​中的wicked_pdf生成PDF

[英]How to generate a PDF using wicked_pdf from an API

Im creating an API that should generate a PDF based on some information on the database. 我创建一个API,应该根据数据库中的一些信息生成PDF。

When trying to call the action im getting an error: 尝试调用操作时出错:

ActionController::UnknownFormat (ActionController::UnknownFormat):
app/controllers/v1/trips_controller.rb:56:in `print_monthly_trips'

This is my controllers: 这是我的控制器:

/#application_controller.rb
class ApplicationController < ActionController::API
  include Response
  include ExceptionHandler
  include Pundit
  include ActionController::MimeResponds

 /#trips_controler.rb
def print_monthly_trips

  @trips_to_print = current_user.trips_for_month(3)

  respond_to do |format|
    format.html
    format.pdf do
      render pdf: "file_name",
      template: "trips/report.html.erb",
      layout: 'pdf.html'
    end
    format.json do
      render pdf: "file_name",
      template: "trips/report.html.erb",
      layout: 'pdf.html'
    end
  end
end

My routes: 我的路线:

get 'print_monthly_trips', to: 'trips#print_monthly_trips'

Im calling my API with: 我用以下方法调用我的API:

http GET https://localhost/print_monthly_trips Accept:'application/vnd.trips.v1+json' Authorization:'my_token'

So, why im getting this: 所以,为什么我得到这个:

ActionController::UnknownFormat (ActionController::UnknownFormat): ActionController :: UnknownFormat(ActionController :: UnknownFormat):

app/controllers/v1/trips_controller.rb:56:in `print_monthly_trips' app / controllers / v1 / trips_controller.rb:56:在'print_monthly_trips'中

Rails controllers that inherit from ActionController::API don't have the ability to render views or use view helpers, which is necessary for many WickedPdf use-cases. ActionController::API继承的Rails控制器无法render视图或使用视图助手,这是许多WickedPdf用例所必需的。

You can either move the PDF creating action to another non-API Rails controller that inherits from ActionController::Base instead, or instantiate one in your action like this: 您可以将PDF创建操作移动到另一个继承自ActionController::Base非API Rails控制器,或者在您的操作中实例化一个,如下所示:

def print_monthly_trips
  pdf_html = ActionController::Base.new.render_to_string(template: 'trips/report.html.erb', layout: 'pdf.html')
  pdf = WickedPdf.new.pdf_from_string(pdf_html)
  send_data pdf, filename: 'file_name.pdf'
end

If you don't want to incur the overhead to instantiate ActionController::Base just to generate a PDF, you may need to make some adjustments to your template, and build the HTML directly with ERB or Erubis something like this: 如果您不想仅仅为了生成PDF而产生实例化ActionController::Base的开销,您可能需要对模板进行一些调整,并使用ERB或Erubis直接构建HTML,如下所示:

def print_monthly_trips
  layout = Erubis::Eruby.new(File.read(Rails.root.join('app/views/layouts/pdf.html.erb')))
  body = Erubis::Eruby.new(File.read(Rails.root.join('app/views/trips/report.html.erb')))
  body_html = body.result(binding)
  pdf_html = layout.result(body: body_html) # replace `yield` in layout with `body`
  pdf = WickedPdf.new.pdf_from_string(pdf_html)
  send_data pdf, filename: 'file_name.pdf'
end

But be aware you won't have access to view helpers, and most wicked_pdf_asset helpers this way. 但请注意,您无法以这种方式访问​​查看帮助程序和大多数wicked_pdf_asset帮助程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM