Emailing a WickedPDF in landscape orientation
Asked Answered
H

2

7

I'm trying to email a .pdf in landscape format. I've been using the WickedPdf.new.pdf_from_string method to attempt to do this. Even though I specify landscape format, the emailed PDF is in portrait.

If someone could show me a way to accomplish this, I'd be most appreciative.

Mailer:

class ProjectMailer < ActionMailer::Base
  def email_mindmap_process(project, user, unused_list_items_column1, unused_list_items_column2)
    @project = project
    @user = user
    mail(:subject => "You've received a MindMap from #{@user.email}!", :to => @project.destination_email) do |format|
      format.pdf do
        attachments["#{@project.title}.pdf"] = WickedPdf.new.pdf_from_string(render_to_string(:pdf => "MindMap", :template => 'projects/show.pdf.html.erb', :orientation => 'Landscape', :locals => {:project => @project, :user => @user }))
      end
    end
  end
end

Controller:

def email_mindmap_process
  @project = Project.find(params[:id])
  @user = current_user
  begin
    ProjectMailer.email_mindmap_process(@project, @user, @unused_list_items_column1, @unused_list_items_column2).deliver
    flash[:notice] = "We have emailed your MindMap to #{@project.destination_email}"
  rescue Net::SMTPAuthenticationError, Net::SMTPServerBusy, Net::SMTPSyntaxError, Net::SMTPFatalError, Net::SMTPUnknownError => e
    flash[:error] = "There was a problem emailing #{@project.destination_email}. Please double-check the address"
  end
end
Hub answered 16/9, 2013 at 16:36 Comment(0)
H
18

I contacted the creator of the WickedPDF gem, Miles Sterrett, and he recommended the following, which worked perfectly!

attachments["#{@project.title}.pdf"] = WickedPdf.new.pdf_from_string(render_to_string(:pdf => "MindMap", :template => 'projects/show.pdf.html.erb',  :locals => {:project => @project, :user => @user }), {:orientation => 'Landscape'})

Big thanks to Miles for responding promptly, and for building such a useful gem!

Hub answered 28/9, 2013 at 8:51 Comment(0)
I
0

I wanted to share a solution that I found that was not using render_to_string. I'm parsing the erb file beforehand then passing that string as the first arg in pdf_from_string.

Example

parsed_content = File.read("#{Rails.root}/app/views/content.erb")
render = ERB.new(parsed_content)
content = render.result(binding)

pdf = WickedPdf.new.pdf_from_string(
  content,
  :orientation => 'Landscape'
)

I hope that this is helpful to someone.

Isaac answered 15/6, 2018 at 13:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.