This is an archived forum post. The information may be outdated. Contact us if you have any questions.

PDFCrowd gem causes deadlock while converting URL to PDF

highway11 wrote on 2015-10-08:
I'm using your Ruby On Rails gem and the following example code. I want to be able to convert a URL that exists on the same webserver and deliver it immediately to the user's web browser.

def generatePdf
# create an API client instance
client ="username", "key")

# convert a web page and store the generated PDF to a variable
pdf = client.convertURI("http://mywebsite/url/to/convert")

# send the generated PDF
:filename => "google_com.pdf",
:type => "application/pdf",
:disposition => "attachment")
rescue Pdfcrowd::Error => why
render :text => why

The problem is if I specify my own website as the url it causes what appears to be a deadlock. The web server appears to be waiting for the pdfcrowd service to access the url to convert it to a pdf, but the web server is already waiting because it is in the middle of a request already.

I've found a workaround for this by calling this code within an aysnchronous process using the sucker_punch gem. However, this is really ugly, as I don't know when it is finished, and have resorted to using a timer in javascript to make the second call to download the PDF.

The strange thing is the code above works sometimes in production (Rails 4.02/Apache/Passenger) but other times it will just hang. In development on Webbrick server it always hangs.

Is there a way I can elegantly run this code and serve up a PDF file without having to resort to using asynchronous processing such as sucker_punch or delayed_job?
support wrote on 2015-10-09:

You can a "pdf" variable to the URL

If pdf=1, your application will call the API with http://mywebsite/url/to/convert?pdf=0
If pdf=0, your application will render the page content