rposborne/wkhtmltopdf-heroku

Invalid chars in expression when running wkhtmltopdf-linux-amd64

drusepth opened this issue · 4 comments

Hey all!

I'm trying to get wicked_pdf running on Heroku with wkhtmltopdf-heroku but each time I try to render a PDF I get an error (below) complaining about invalid hex chars in whatever expression is being evaluated. I'm on the Heroku-20 stack.

My WickedPdf config is this:

WickedPdf.config ||= {}
WickedPdf.config.merge!({
  :exe_path => Rails.root.join('vendor/bundle/bin/wkhtmltopdf-linux-amd64').to_s,
})

and I've confirmed the binary is in the correct place in a heroku run bash console:

~ $ ls /app/vendor/bundle/bin/ | grep wkhtmltopdf
wkhtmltopdf-linux-amd64
~ $ which wkhtmltopdf-linux-amd64
/app/vendor/bundle/bin/wkhtmltopdf-linux-amd64

In debugging, I noticed that I get the same error when I just try to run the wkhtmltopdf binary directly. The error is here:

~ $ wkhtmltopdf-linux-amd64 
Traceback (most recent call last):
	1: from /app/vendor/bundle/bin/wkhtmltopdf-linux-amd64:29:in `<main>'
/app/vendor/bundle/bin/wkhtmltopdf-linux-amd64:29:in `load': /app/vendor/bundle/ruby/2.7.0/gems/wkhtmltopdf-heroku-2.12.6.0/bin/wkhtmltopdf-linux-amd64:1: Invalid char `\\x7F' in expression (SyntaxError)
/app/vendor/bundle/ruby/2.7.0/gems/wkhtmltopdf-heroku-2.12.6.0/bin/wkhtmltopdf-linux-amd64:1: Invalid char `\\x02' in expression
/app/vendor/bundle/ruby/2.7.0/gems/wkhtmltopdf-heroku-2.12.6.0/bin/wkhtmltopdf-linux-amd64:1: Invalid char `\\x01' in expression
/app/vendor/bundle/ruby/2.7.0/gems/wkhtmltopdf-heroku-2.12.6.0/bin/wkhtmltopdf-linux-amd64:1: Invalid char `\\x01' in expression
/app/vendor/bundle/ruby/2.7.0/gems/wkhtmltopdf-heroku-2.12.6.0/bin/wkhtmltopdf-linux-amd64:1: Invalid char `\\x03' in expression

Is this a problem with installation or how I'm using the binary? I've attempted to remove the gem and reinstall it and always get the same issue. Is there some sort of configuration or settings I need to change after adding the gem?

Here's a snippet of the relevant Ruby code that I'm using (which produces the same error):

    formatted_pdf = "<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /></head><body><h1>Hello</h1></body></html>" # PdfService.html_for(request)
    WickedPdf.new.pdf_from_string(formatted_pdf)

Here's the full error again when thrown from Ruby instead of running the binary manually:

2021-03-30T01:22:13.516103+00:00 app[web.1]: [da470d9f-4615-470f-b8c3-9eafe183c8d7] RuntimeError (Failed to execute:
2021-03-30T01:22:13.516105+00:00 app[web.1]: ["/app/vendor/bundle/bin/wkhtmltopdf-linux-amd64", "file:////tmp/wicked_pdf20210330-36-yev3hs.html", "/tmp/wicked_pdf_generated_file20210330-36-1fhyurd.pdf"]
2021-03-30T01:22:13.516106+00:00 app[web.1]: Error: PDF could not be generated!
2021-03-30T01:22:13.516107+00:00 app[web.1]: Command Error: /app/vendor/bundle/bin/wkhtmltopdf-linux-amd64:29:in `load': /app/vendor/bundle/ruby/2.7.0/gems/wkhtmltopdf-heroku-2.12.6.0/bin/wkhtmltopdf-linux-amd64:1: Invalid char `\\x7F' in expression (SyntaxError)
2021-03-30T01:22:13.516108+00:00 app[web.1]: /app/vendor/bundle/ruby/2.7.0/gems/wkhtmltopdf-heroku-2.12.6.0/bin/wkhtmltopdf-linux-amd64:1: Invalid char `\\x02' in expression
2021-03-30T01:22:13.516109+00:00 app[web.1]: /app/vendor/bundle/ruby/2.7.0/gems/wkhtmltopdf-heroku-2.12.6.0/bin/wkhtmltopdf-linux-amd64:1: Invalid char `\\x01' in expression
2021-03-30T01:22:13.516109+00:00 app[web.1]: /app/vendor/bundle/ruby/2.7.0/gems/wkhtmltopdf-heroku-2.12.6.0/bin/wkhtmltopdf-linux-amd64:1: Invalid char `\\x01' in expression
2021-03-30T01:22:13.516110+00:00 app[web.1]: /app/vendor/bundle/ruby/2.7.0/gems/wkhtmltopdf-heroku-2.12.6.0/bin/wkhtmltopdf-linux-amd64:1: Invalid char `\\x03' in expression
2021-03-30T01:22:13.516110+00:00 app[web.1]: from /app/vendor/bundle/bin/wkhtmltopdf-linux-amd64:29:in `<main>'
2021-03-30T01:22:13.516111+00:00 app[web.1]: ):
2021-03-30T01:22:13.516111+00:00 app[web.1]: [da470d9f-4615-470f-b8c3-9eafe183c8d7]

This is the SHA1 of the binary I'm attempting to use:

~ $ sha1sum /app/vendor/bundle/bin/wkhtmltopdf-linux-amd64
e673101f6d67a69ce5e184dad859f42f2dd255bb  /app/vendor/bundle/bin/wkhtmltopdf-linux-amd64

Any thoughts would be much appreciated! PDF generation works properly locally (using wkhtmltopdf-binary-edge instead of wkhtmltopdf-heroku) but debugging Heroku-specific issues is always a bit rough. ☃️

Hi - are you running heroku-20 stack? or 18?

this seems to be an error from the https://github.com/wkhtmltopdf/wkhtmltopdf side - this gem simply provides the binary.

btw- this gem configures exe_path automatically - you probably don't need that in your initializer file.

I noticed this happening in my local environment if I explicitly run the binary generated by RubyGems, which looks like this:

#!/usr/bin/env ruby
#
# This file was generated by RubyGems.
#
# The application 'wkhtmltopdf-heroku' is installed as part of a gem, and
# this file is here to facilitate running it.
#

require 'rubygems'

version = ">= 0.a"

str = ARGV.first
if str
  str = str.b[/\A_(.*)_\z/, 1]
  if str and Gem::Version.correct?(str)
    version = str
    ARGV.shift
  end
end

if Gem.respond_to?(:activate_bin_path)
load Gem.activate_bin_path('wkhtmltopdf-heroku', 'wkhtmltopdf-linux-amd64', version)
else
gem "wkhtmltopdf-heroku", version
load Gem.bin_path("wkhtmltopdf-heroku", "wkhtmltopdf-linux-amd64", version)
end

If I bypass this binary and go directly to the one provided by the gem, it works fine. The issue should go away if you set the path on Wicked directly to the binary, or just let the gem do it for you.

@drusepth Were you able to figure out a solution to this?

@meer-online I was unable to get it working at the time and ended up using wkhtmltopdf-binary instead (details).