Ruby on Rails 7 rendering .rtf.erb模板

63lcw9qa  于 2023-10-18  发布在  Ruby
关注(0)|答案(1)|浏览(116)

我是Ruby on Rails的新手,我正在尝试渲染.rtf.erb模板,我知道它需要一些第三方插件或库,我发现了像rtf gem libraryrrtf library这样的东西,但我希望以不同的方式做一些事情。
我在./views/order.rtf.erb模板,我发送/渲染订单的详细信息,这个模板,并希望在RTF格式下载。
内容看起来像下面的ruby标签:

.
.
.
\trowd \trgaph108\trleft-108\trbrdrt\brdrs\brdrw10 \trbrdrl\brdrs\brdrw10 \trbrdrb\brdrs\brdrw10 \trbrdrr\brdrs\brdrw10 \trbrdrh\brdrs\brdrw10 \trbrdrv\brdrs\brdrw10 \clvertalt\clbrdrt\brdrs\brdrw10 \clbrdrl\brdrs\brdrw10 \clbrdrb\brdrs\brdrw10 \clbrdrr
\brdrs\brdrw10 \cltxlrtb \cellx3690\clvertalt\cltxlrtb \cellx5940\clvertalt\clbrdrt\brdrs\brdrw10 \clbrdrl\brdrs\brdrw10 \clbrdrb\brdrs\brdrw10 \clbrdrr\brdrs\brdrw10 \cltxlrtb \cellx8910\pard \nowidctlpar\widctlpar\intbl\adjustright {\fs16 
\par }\pard\plain \s15\nowidctlpar\widctlpar\intbl\adjustright \fs16\cgrid {\f1 SOLD TO:
\par }\pard\plain \nowidctlpar\widctlpar\intbl\adjustright \fs20\cgrid {\f1 
\par <%= @client_addr.addr1 rescue 'N/A'%>
\par <%= @client_addr.addr2 rescue 'N/A'%>
\par <%= @client_addr.addr3 rescue 'N/A'%>
\par <%= @client_addr.city rescue 'N/A'%>, <%= @client_addr.state rescue 'N/A'%> <%= @client_addr.zipcode rescue 'N/A'%>
\par }{\fs16 \cell }{\cell }{\fs16 
\par }\pard\plain \s15\nowidctlpar\widctlpar\intbl\adjustright \fs16\cgrid {\f1 SHIPPED TO:
\par }\pard\plain \nowidctlpar\widctlpar\intbl\adjustright \fs20\cgrid {\f1 
\par <%= @shipping_addr.addr1 rescue 'N/A'%>
\par <%= @shipping_addr.addr2 rescue 'N/A'%>
\par }\pard\plain \s16\nowidctlpar\widctlpar\intbl\adjustright \fs20\cgrid {\f1 <%= @shipping_addr.addr3 rescue 'N/A'%>
\par }\pard\plain \nowidctlpar\widctlpar\intbl\adjustright \fs20\cgrid {\f1 <%= @shipping_addr.city rescue 'N/A'%>, <%= @shipping_addr.state rescue 'N/A'%> <%= @shipping_addr.zipcode rescue 'N/A'%>
\par }{\fs16 \cell }\pard \nowidctlpar\widctlpar\intbl\adjustright {\row }

\pard \nowidctlpar\widctlpar\adjustright { \par }
.
.
.

我的控制器功能:

def show
    @order = Order.find(params[:id])
    raise NotFound unless @order
    if params[:format] == 'rtf'
      latest_order = Order.find_by(order_type: 'o', customer_id: @order.customer_id)
      if latest_order.nil?
        @client_addr = Customer.find(@order.customer_id).addresses[0]
        @shipping_addr = @client_addr
      else
        @client_addr = latest_order.client_address
        @shipping_addr = latest_order.shipping_address
      end

      # Construct an object representing the RTF document
      rtf = RRTF::Document.new

      #==========================================
      #here I do not have any idea what to do...
      #==========================================
      
      #what i did
      rtf = File.read(Rails.root.to_s+ "/app/views/order.rtf.erb")
      # Convert document into RTF string
      send_data rtf, filename: 'order', type: 'application/rtf'

      #render template: "order.rtf.erb", formats: :rtf, layout: false
  else
    render
  end
end

输出:x1c 0d1x

ndh0cuux

ndh0cuux1#

如果RTF文件不是动态生成的,则不需要额外的库。在您的示例中,您有一个带有嵌入式ERB的RTF文件,因此问题只是将该文件放入ERB并让它发挥其魔力。
类似这样的东西应该可以工作:

def show
  # ...

  rtf = File.read(File.join(Rails.root, "/app/views/order.rtf.erb"))
  # Load the file as an ERB template
  erb_template = ERB.new(rtf)
  # Executes the generated ERB code to produce a completed template
  output = erb_template.result(binding)
  # Convert document into RTF string
  send_data output, filename: 'order', type: 'application/rtf'

  # ...
end

相关问题