'opening up a pdf in AWS Cloud9 environment

I am trying to run a Flask / Python / MySQL web app in Amazon AWS Cloud9, so I can make sure it works before I put it online. It works good, except for one thing. In one point in my application, a new tab opens up with a pdf conversion of a page. The pdf file is not static, it is generated using an html page. When I try to open up this page as a pdf in the virtual environment, it gives me an error:

OSError: wkhtmltopdf: cannot connect to X server

It suggests I go to a link for more information: https://github.com/JazzCore/python-pdfkit/wiki/Using-wkhtmltopdf-without-X-server

Then it starts off by saying I run these two commands:

apt-get install wkhtmltopdf

apt-get install xvfb

"apt-get" did not work when I tried to run it from the cloud9 terminal, so I used the command "sudo yum install whtmltopdf" instead. wkhtmltopdf was successful, and I have version 0.12.4 now installed. "sudo yum install xvfb" however, does not install anything.

Without being able to install xvfb, I cannot follow the next steps, which are executing the commands:

printf '#!/bin/bash\nxvfb-run -a --server-args="-screen 0, 1024x768x24" /usr/bin/wkhtmltopdf -q $*' > /usr/bin/wkhtmltopdf.sh

chmod a+x /usr/bin/wkhtmltopdf.sh ln -s /usr/bin/wkhtmltopdf.sh /usr/local/bin/wkhtmltopdf

wkhtmltopdf http://www.google.com output.pdf

So, my question is... how can I get this pdf tab to open?

This is the part of my app.py code that opens up a new tab with the pdf conversion ("pdfkit" has been imported)

        @app.route("/print", methods=["GET"])
        @login_required
        def printPDF():
                renderedPDF = render_template("print.html", signWidth=int(request.args['signWidth'].split("px")[0]), signHeight=request.args['signHeight'], rows=ast.literal_eval(request.args['rows']), rowStrings=ast.literal_eval(request.args['rowStrings']), lettersPile=request.args['lettersPile'], newMessage=request.args['newMessage'], lettersNeeded=ast.literal_eval(request.args['lettersNeeded']), lettersPulled=ast.literal_eval(request.args['lettersPulled']))
                pdf = pdfkit.from_string(renderedPDF, False)
        
                response = make_response(pdf)
                response.headers['Content-Type'] = 'application/pdf'
                response.headers['Content-Disposition'] = 'inline; filename=signChange.pdf'
        
                return response

Is there ay way I can get this "x server" to work, or is there another way to get my pdf opened in this app without causing any error?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source