wicked_pdf cover page stops rest of PDF generation - ruby-on-rails

Working on a PDF and (in both development and production) the PDF will render only the cover page:
https://www.patchvault.org/lodges/456a-powhatan/issues/checklist.pdf
Debug shows the rest of the content, without the cover page:
https://www.patchvault.org/lodges/456a-powhatan/issues/checklist.pdf?debug=true
This intermittently will render fine, or show subsequent pages without images (typical of when it does render), but mostly it just renders the cover. Disabling the cover in the controller action gets the other pages to render as expected. Here's the controller action (I have attachment disabled for development purposes):
def checklist
# PDF Checklist of issues from a lodge
#issues = #issueable.issues.non_event_issues.ordered_issues
#event_issues = #issueable.issues.event_issues.order(issue_number: :asc)
respond_to do |format|
format.html
format.pdf do
render pdf: "#{#issueable.slug}_checklist",
# disposition: 'attachment',
template: 'issues/checklist.pdf.erb',
cover: render_to_string('issues/checklist_cover.pdf.erb'),
dpi: '150',
background: true,
header: {html: {template: 'layouts/_checklist_header'}, spacing: 5 },
footer: {html: {template: 'layouts/_checklist_footer'}, spacing: 0 },
margin: {top: 30, bottom: 20, left: 10, right: 10},
show_as_html: params.key?('debug')
end
end
end
and here's is the output in development to wkhtmltopdf:
"***************[\"/Users/jathayde/Development/Meticulous/Products/patchvault/vendor/cache/ruby/2.4.0/gems/wkhtmltopdf-binary-0.12.3.1/bin/wkhtmltopdf\", \"-q\", \"--dpi\", \"150\", \"--margin-top\", \"30\", \"--margin-bottom\", \"20\", \"--margin-left\", \"10\", \"--margin-right\", \"10\", \"--header-spacing\", \"5\", \"--header-html\", \"file:////var/folders/25/q7y1f5px3xz8bw7sdzcyrz_c0000gn/T/wicked_header_pdf20180312-17436-12xyhl7.html\", \"--footer-spacing\", \"0\", \"--footer-html\", \"file:////var/folders/25/q7y1f5px3xz8bw7sdzcyrz_c0000gn/T/wicked_footer_pdf20180312-17436-eldx10.html\", \"cover\", \"/var/folders/25/q7y1f5px3xz8bw7sdzcyrz_c0000gn/T/wicked_cover_pdf20180312-17436-y1d6wy.html\", \"file:////var/folders/25/q7y1f5px3xz8bw7sdzcyrz_c0000gn/T/wicked_pdf20180312-17436-tr8hae.html\", \"/var/folders/25/q7y1f5px3xz8bw7sdzcyrz_c0000gn/T/wicked_pdf_generated_file20180312-17436-18ftq2q.pdf\"]***************"
The cover is a full HTML page as it wouldn't render styles without being one and calling the CSS file.
Software notes:
Ruby 2.4.0p0
Rails 5.1.5
Patch images are served from S3.
Gems, etc are cached to vendor/cache

The only solution I've found is to remove the Google font call in the cover page, and only load fonts on the inside page template.

Related

Problem with generate pdf and display images

I have a problem with displaying the images in pdf. 5 out of 100 attempts work. After saving all data to database, there is opened a new site with pdf generated by gem wkhtmltopdf. During getting the image from path /rails/active_storage/blobs/... service is stopped and report isn't generated. There are no errors on consoles .Only solution is stopped the server ( ctrl+c ) and start again the server.
The server is stopped on request :
Started GET "/rails/active_storage/blobs/...
I display image by line :
<%= image_tag(#object.image, class: '') if #object.image.attached? %>
There is a method for generate report (above this code there are the varialbes to display in report )
respond_to do |format|
format.html do
redirect_to index
end
format.pdf do
render pdf: "Raport",
template: "myFolder/report.html.erb",
:footer => {
:font_size => '8'
}
end
end
I'm using rails 5.2.3, ruby 2.5.3
I'm expect to generate the pdf with images.

Wicked PDF header not showing up

After already checking the Rails Gem repository for similar issues as well as Stack Overflow, I couldn't find an answer to my problem.
I'm trying to render a pdf using wicked_pdf within a Rails controller, but the header is not showing up, no matter what I do or which recommended solutions to similar issues I follow.
First and foremost, here is the development console output:
***************WICKED***************
Rendering biddings/show.pdf.html.haml within layouts/pdf
Rendered biddings/show.pdf.html.haml within layouts/pdf (0.7ms)
Rendering biddings/header_pdf.html.haml within layouts/pdf_header
Rendered biddings/header_pdf.html.haml within layouts/pdf_header (1.9ms)
"***************[\"/home/tommy/.rvm/gems/ruby-
2.5.1#igalbids/bin/wkhtmltopdf\", \"-q\", \"--encoding\", \"UTF-8\",
\"--javascript-delay\", \"500\",
\"--disable-internal-links\", \"--disable-external-links\",
\"--orientation\", \"Portrait\", \"--margin-top\",
\"50\", \"--margin-bottom\", \"25\", \"--header-html\",
\"file:////tmp/wicked_header_pdf20180801-27285-b8y5sg.html\",
\"--footer-right\", \"Página [page] de [topage]\",
\"file:////tmp/wicked_pdf20180801-27285-1jfgdd7.html\",
\"/tmp/wicked_pdf_generated_file20180801-27285-1bkrvhx.pdf\"]***************"
Rendering text template
Rendered text template (0.1ms)
Sent data Licitación_2524.pdf (0.6ms)
Completed 200 OK in 2334ms (Views: 0.5ms | ActiveRecord: 64.4ms)
As you can see, both the header layout and its contents are being rendered and processed, however they don't make the final output PDF, and I don't know why! Look:
So, here's my controller code:
class Api::V1::Biddings::PdfBiddingsController < PdfController
# JWT Authentication enforced
before_action :authenticate_user!
# GET /biddings/:id/pdf
def show
#bidding = scoped_collection.find(params[:id])
authorize [:biddings, :pdf, #bidding]
respond_to do |format|
format.pdf do
render(
pdf: "#{Bidding.model_name.human}_#{#bidding.code}",
disposition: "inline",
orientation: "Portrait",
template: 'biddings/show.pdf.html.haml',
header: {
html: {
template: "biddings/header_pdf.html.haml",
handlers: [:haml],
layout: "pdf_header",
formats: [:haml, :html]
}
},
footer: {
html: {
handlers: [:haml],
layout: "pdf",
formats: [:haml, :html],
encoding: 'UTF-8'
},
right: "#{I18n.t('pdf.page')} [page] #{I18n.t('pdf.of')} [topage]"
},
margin: { :top => 50, :bottom => 25},
handlers: [:haml],
layout: "pdf",
javascript_delay: 500,
encoding: 'UTF-8',
show_as_html: false,
disable_internal_links: true,
disable_external_links: true) and return
end
end
end
protected
def self.model
Bidding
end
private
def scoped_collection
policy_scope([:biddings, :pdf, Bidding]).includes(:bidding_type, :client, :payment_condition, :price_list, :real_payment_condition, :sales_man, :user)
end
def records_per_page
params[:per_page] || 10
end
end
Nothing fancy, there you can see all the config options, pretty standard. Needless to say, the footer with the page numbering IS working fine (screenshot too long to show, but trust me). Can't say the same about the header.
Here's the PDF header layout file:
pdf_header.html.haml:
!!! 5
%html
%head
%meta{:content => "text/html; charset=utf-8", "http-equiv" => "content-type"}/
= wicked_pdf_stylesheet_link_tag "bidding_pdf", media: :all
= csrf_meta_tags
%body.pdf
= yield
and here the contents for the header "contents" per se:
header_pdf.html.haml:
Test text
Just plain text. I have a Linux 16.04 x64 OS, wicked_pdf (1.1.0), wkhtmltopdf-binary (0.12.4). How can I debug this?
For anyone who bumps in this, since OP's answer is not too precise, what did the job for me was including a DOCTYPE HTML tag in your header/footer. Went from invisible header (with text that could be found using the search tool), to fully rendered.
For anyone else reaching here... it was a CSS issue. The header was there but "invisible" and no matter what margin I set on the render options it was a CSS issue. After starting the CSS from scratch, the header appeared! I could not debug it with the flag show_as_html: true because header and footer are not rendered in that mode, only the body.
If anyone reads this and happens to be in the same situation, use the search tool in the PDF document to find a word you know that's in the header. If it finds something but it's invisible, then you know you have a CSS problem.
Also don't forget to check if you included in the html of the header the <!DOCTYPE html>. Thanks #joaolell for this.
Another thing to check, is that you have the version with patched qt of the wkhtmltopdf library (0.12.4 and above) that supports header and footer. Previous versions won't
Upgrade wkhtmltopdf binary to at least version "0.12.4 (with patched qt)". I just spent half a day troubleshooting because my version of 0.12.1 did not support header and footer.
Ref: Wicked pdf not rendering header/footer

Wicked pdf gem not creating correct data in pdf on rails production server

I am using Wicked pdf gem for creating pdf. It is absolutely working fine on my local. But on production it is giving serious issues. The pdf is generated but in spite of generating a single page it generates 14-15 pages with data like this:
")),this.$.write(e),this.$.close()},find:function(e){return new CKEDITOR.dom.nodeList(this.$.querySelectorAll(e))},findOne:function(e){return(e=this.$.querySelector(e))?new CKEDITOR.dom.element(e):null},_getHtml5ShivFrag:function(){var e=this.getCustomData("html5ShivFrag");return e||
(e=this.$.createDocumentFragment(),CKEDITOR.tools.enableHtml5Elements(e,!0),this.setCustomData("html5ShivFrag",e)),e}}),CKEDITOR.dom.nodeList=function(e){this.$=e},CKEDITOR.dom.nodeList.prototype={count:function(){return this.$.length},getItem:function(e){return
0>e||e>=this.$.length?null:(e=this.$[e])?new CKEDITOR.dom.node(e):null}},CKEDITOR.dom.element=function(e,t){"string"==typeof e&&(e=(t?t.$:document).createElement(e)),CKEDITOR.dom.domObject.call(this,e)},CKEDITOR.dom.element.get=function(e){return(e="string"==typeof e?
document.getElementById(e)||document.getElementsByName(e)[0]:e)&&(e.$?e:new CKEDITOR.dom.element(e))},CKEDITOR.dom.element.prototype=new CKEDITOR.dom.node,CKEDITOR.dom.element.createFromHtml=function(e,t){var n=new CKEDITOR.dom.element("div",t);return
n.setHtml(e),n.getFirst().remove()},CKEDITOR.dom.element.setMarker=function(e,t,n,i){var
r=t.getCustomData("list_marker_id")||t.setCustomData("list_marker_id",CKEDITOR.tools.getNextNumber()).getCustomData("list_marker_id"),o=t.getCustomData("list_marker_names")||t.setCustomData("list_marker_names",{}).getCustomData("list_marker_names");return
e[r]=t,o[n]=1,t.setCustomData(n,i)},CKEDITOR.dom.element.clearAllMarkers=function(e){for(var t in e)CKEDITOR.dom.element.clearMarkers(e,e[t],1)},CKEDITOR.dom.element.clearMarkers=function(e,t,n){var
i,r=t.getCustomData("list_marker_names"),o=t.getCustomData("list_marker_id");for(i in r)t.removeCustomData(i);t.removeCustomData("list_marker_names"),n&&(t.removeCustomData("list_marker_id"),delete e[o])},function(){function e(e,t){return-1<(" "+e+" ").replace(o," ").indexOf(" "+t+"
")}function t(e){var t=!0;return e.$.id||(e.$.id="cke_tmp_"+CKEDITOR.tools.getNextNumber(),t=!1),function(){t||e.removeAttribute("id")}}function n(e,t){var n=CKEDITOR.tools.escapeCss(e.$.id);return"#"+n+" "+t.split(/,\s*/).join(", #"+n+" ")}function i(e){for(var
t=0,n=0,i=a[e].length;i>n;n++)t+=parseInt(this.getComputedStyle(a[e][n])||0,10)||0;return t}var r=document.createElement("_").classList,r="undefined"!=typeof r&&null!==String(r.add).match(/[Native code]/gi),o=/[\n\t\r]/g;CKEDITOR.tools.extend(CKEDITOR.dom.element.prototype,
{type:CKEDITOR.NODE_ELEMENT,addClass:r?function(e){return this.$.classList.add(e),this}:function(t){var n=this.$.className;return n&&(e(n,t)||(n+=" "+t)),this.$.className=n||t,this},removeClass:r?function(e){var t=this.$;retur
The entire 14-15 pages are like this.
This is the method that creates pdf.
def generate_supplier_commission_pdf
#start_date = params[:start_date].to_date.strftime('%d/%m/%Y')
#end_date = params[:end_date].to_date.strftime('%d/%m/%Y')
if params[:sec_filter].present?
sec_filter = true
else
sec_filter = false
end
results = get_report_results_by_type(params[:report_type], #start_date, #end_date, sec_filter)
#results = JSON.parse(results)
#type = params[:report_type].to_i
respond_to do |format|
format.html
format.pdf do
render pdf: "report",
layout: 'pdf_layout',
template: 'reports/generate_supplier_commission_pdf.html.erb',
encoding: 'UTF8',
print_media_type: true,
disposition: 'attachment',
page_size: 'letter',
orientation: 'landscape',
lowquality: 'false',
debug: true
end
end
end
Each and everything like the wkhtml path or anything that can be taken of care of in the code is fine. The only difference I saw is in the logs i.e after this line
***************WICKED***************
Rendered reports/generate_supplier_commission_pdf.html.erb within layouts/pdf_layout (5.2ms)
The following line is not present on server logs.
"***************[\"/usr/bin/wkhtmltopdf\", \"-q\", \"--orientation\", \"landscape\", \"--page-size\", \"letter\", \"--encoding\", \"UTF8\", \"--lowquality\", \"--print-media-type\", \"file:///tmp/wicked_pdf20160903-24256-1v1ay70.html\", \"/tmp/wicked_pdf_generated_file20160903-24256-1da8k68.pdf\"]***************"
I will be really grateful if someone could point out what is going wrong in this case. Thanks in advance.
I managed to resolve this issue by commenting this line in my config/environments/production.rb
# config.assets.js_compressor = :uglifier
and adding this line in it's place:
config.assets.compress = true

Failed to execute: Error: "\xFE" from ASCII-8BIT to UTF-8

Rails 4
*Mac OSX 10.8.4*
I'm using the following Gem for wicked_pdf pdf generation:
gem 'wkhtmltopdf-binary'
gem 'wicked_pdf'
Rendering Views as pdfs works fine and Google displays it's PDF viewer correctly. My PDFs look exactly how I want them to.
The problem arises when I try to save a pdf to disc, for the purpose of emailing them to a user.
For example, this works fine:
def command
#event = Event.find(params[:id])
#client = Contact.find(#event.client_id)
#organizer = Contact.find(#event.organizer_id)
render layout: 'command',
pdf: 'Event Command',
show_as_html: params[:debug].present?,
dpi: 300,
print_media_type: true,
margin: {
top: 0,
bottom: 0,
left: 0,
right: 0
}
end
That will render the pdf in the Google Chrome PDF viewer.
But here, is where I want to generate a PDF and save to file.
def send_email
#event = Event.find(params[:id])
#client = Contact.find(#event.client_id)
#organizer = Contact.find(#event.organizer_id)
proforma = render_to_string(
pdf: 'proforma.pdf',
template: 'events/proforma',
layout: 'proforma'
)
pdf = WickedPdf.new.pdf_from_string(
proforma
)
save_path = Rails.root.join('public','proforma.pdf')
File.open(save_path, 'wb') do |file|
file << pdf
end
end
But I'm getting the error:
Failed to execute:
Error: "\xFE" from ASCII-8BIT to UTF-8
Try this:
File.open(save_path, 'w:ASCII-8BIT') do |file|
file << pdf
end
The PDF rendered as a string in memory seems to be in ASCII, so save it as such :)

wicked_pdf using image tag helper in css file

I'm using the wicked_pdf plugin with a rails 3.2 project and the asset pipeline. I want to define my picture inside pdf.css.erb
Here is my controller code to make use of a layout file:
respond_to do |format|
format.html
format.pdf do
render :pdf => "Cars.pdf",
:template => "reports/cars.html.haml",
:page_size => "A4",
:layout => "pdf.html",
:handlers => [:haml]
end
end
Here is my layout file where I include my pdf.css.erb file.
!!!
%html
%head
= wicked_pdf_stylesheet_link_tag "pdf"
= csrf_meta_tag
%body
.contentbg
.logo
.content= yield
Lastly here is my CSS
.logo {
background: wicked_pdf_image_tag "logo.jpg";
width: 100px;
height: 80px;
}
This does not find the logo.jpg. If I put the wicked_pdf_image_tag "logo.jpg" line directly in my pdf.html.haml template layout file then it does render my logo in the pdf generated.
Is it possible to use the wicked_pdf_image_tag helper inside a css.erb file?
That's not going to work inside a css.erb file, firstly because it is missing erb tags, but mostly because wicked_pdf_image_tag will expand that out to this:
.logo {
background: <img src="file://foo/bar/public/images/logo.jpg">;
}
Which is not valid css.
Best bet would be to construct it by hand and something like this:
.logo {
background: url(<%= Rails.root.join('assets','images','logo.jpg').to_s %>);
}
The reason to have the direct file access path is so wkhtmltopdf doesn't have to make a web request to get the assets, hence load more quickly.

Resources