pdf file rendered in portrait instead of landscape

1.3k Views Asked by At

We are using princely gem to generate pdf documents on our rails 4 app. For the last couple of days, the generated pdf is rendering in portrait mode instead of the specified landscape mode; this functionality has been working fine for years.

There have been some updates being done on the ruby/rails versions on the server, but nothing has been changed on the pdf generation code itself.

One interesting aspect is that the pdf is generated in landscape mode on the development server, when I test it locally on my machine(Mac). The same code generates pdf in portrait mode on the staging/production server (Linux).

Related code:

From Gemfile

gem 'rails', '4.2.5.1'
gem 'princely', git: "[email protected]:mbleigh/princely.git"
....

Pdf render code:

render :pdf => "ticket",
       :template => "admin/tickets/print.pdf.erb",
       :stylesheets => ["printpdf.css"]

app/assets/stylesheets/printpdf.css:

@page {                                                                            
  size: 8.5in 11in landscape;                                                      
  margin: 5mm 5mm 5mm 5mm;                                                         
}                                                                                  
@page {                                                                            
  @bottom-right {                                                                  
    content: counter(page);                                                        
  }                                                                                
}                                                                                  
#pagebreak {                                                                       
  page-break-after: always;                                                        
}    

Princely command executed (as shown in the log):

PRINCE XML PDF COMMAND
/usr/local/bin/prince --input=html --server --log=/Users/prakash/Projects/main_project/log/prince.log  -s /Users/prakash/Projects/main_project/app/assets/stylesheets/printpdf.css  --silent - -o -

Rails version was changed from 4.2.4 to 4.2.5.1 recently, and ruby version was changed from 2.2.0 to 2.3.0. I rolled back both these changes on the staging server, but reverting neither of those changes fixes the portrait-instead-of-landscape issue.

Would appreciate any suggestions on where to look for the root cause of this problem, and how to fix it.

2

There are 2 best solutions below

0
On BEST ANSWER

Final Solution

This was fixed by downgrading sprockets-rails gem from 3.0.0 to 2.3.3 because of this issue.

How I got there

Found an error message in prince.log file generated by the princexml command line utility which said printpdf.css: warning: can't open input file: No such file or directory

That made me realize that the princexml commandline utility was being passed printpdf.css as the value for the css file, instead of the fully-qualified path: /Users/prakash/Projects/main_project/app/assets/stylesheets/printpdf.css on staging.

Debugging through princely code, found that Rails.application.assets is being returned as nil here.

One other interesting thing I noticed during this exercise:

Downgrading rails version from 4.2.5.1 to 4.2.4 originally to rollback all my changes did not downgrade the version of sprockets-rails gem to 2.3.3; it still kept it at 3.0.0; hence my conclusion that the rails upgrade step was not a culprit for this problem.

0
On

If you can't get this right with princely take a look at combine_pdf, here a sample that rotates a pdf file and saves it into another file.

require "combine_pdf"

pdf = CombinePDF.load("file.pdf")
pdf.pages.each {|p| p.orientation :landscape }
pdf.save 'my.pdf'