Azure websites and wkhtmltopdf
Asked Answered
E

2

21

So after beginning my azure websites-adventure, I have been encountering disappointment after disappointment to the point of 'almost' regretting taking the azure route..the latest: my app uses wkhtmltopdf(Rotativa) for all its PDF generation. And as I discovered now during my staging testing...that doesnt work. According to most info on the web, azure websites cannot run .exe's. However, most info I've seen is by now 2 years old and I know Azure development is going fast.

Is there by now a way to run wkhtmltopdf on azure websites, or a workaround atleast? I cannot really find a free alternative like wkhtmltopdf to seems to be working.(Itextsharp doesnt seem to like my html, so thats no option).

Emelun answered 23/2, 2015 at 19:48 Comment(13)
it is possible to host an .exe as a WebRole, and call your WebRole from your Website.Isoprene
The following post may be helpful: codeproject.com/Articles/331425/…Venous
@Will, that would be more work than just giving up azureEmelun
@Will, yes it is possible to run exe's on an azure website. However, there are certain APIs that are restricted and won't work (anything that requires admin access for example)Carniola
@ZainRizvi after a minimal test, I withdraw my claim and am heading to the store for lighter fluid and marshmallows. As for the OP, he could find out in five minutes by publishing a test website that converts a static html file using the exe. You just have to add the exe as a resource, then File.WriteAllBytes to the working directory (typeof(ExecutingController).Assembly.Location minus the filename) and spawn a process to execute it. Whether it gets bounded by other security measures I won't hazard a guess. Did that. Looked stupid.Moor
I should have clarified, running an exe was part of the problem, wkhtmltopdf apparently uses windows GDI lib, which seems to forbidden to use..Emelun
perhaps you should rephrase your question, providing error messages or information that has led you to the conclusion that windows GDI lib is forbidden. as it's stated now, it's not really clear what isn't working, so providing an alternative isn't very likely.Isoprene
although, about 2 minutes of google turned up github.com/gmanny/PechkinIsoprene
@claies, i started looking into that 30min ago :), it is not rending my css (MVC app) though...not sure if its compatibleEmelun
Went into the same problem. wkhtmltopdf actually runs fine when spawn as an exe with static resources as well as with data piped in through the standard input. It, however, does not work with a URL. Which means that whenever I put a URL for wkhtmltopdf to render, the process just hangs. It does not stop, it just continues forever without outputting anything. Therefore, the culprit seems to be with web access.Cato
However, the PDF created will not have the proper fonts. Every single character is rendered as a black square, and there are no fonts referred to by the PDF file. It appears that GDI calls are failing on Azure so that the enbedded WebKit cannot render the page.Cato
Embedding a stylesheet or a script within the HTML passed into wkhtmltopdf will cause it to hang again. So it definitely looks like web access is stalling the process forever.Cato
I'm the creator of Rotativa. I developed a service with the same Rotativa usage but you can use without having to worry about installing executables and having special permissions. It's online at rotativahq.comTraprock
E
8

Running wkhtmltopdf in Azure Websites in a simple way seems to be impossible, still. Static .NET wrappers like Pechkin wont work either in WebSites, there are very few alternatives that are 1. free and 2. dont use wkhtmltopdf or similar under the hood. Most alternatives like iTestSharp are not very advanced when it comes to html/css3/javascript reading (as I do some design changing with .js on page load).

I ended up creating a Azure Cloud Service, that runs wkhtmltopdf.exe without any issues. I send the html to the service, and get a byte[] in return. So far this seems to be working fine. Hope this helps others with similar problems.

Emelun answered 24/2, 2015 at 17:15 Comment(14)
FYI - the problem is not in running an exe vs using a .NET wrapper. Also there is a pretty good chance that alternatives to wkhtmltopdf won't work either. the root cause for wkhtmltopdf.exe not working is that most PDF converters use native Windows GDI+ API calls that are blocked in Azure Websites sandbox. Any tool using these API calls, which are responsible for rendering fonts and things like that, will be blocked. Creating an external service and using it just for the pdf generation is probably the best you can doHeadrail
Good comment, I figured that out later as well, my mind was fried after trying to make it work for 12hrs :)Emelun
Hi NeedACar, I am new to Azure and I recently hit the same issue. Is it possible for you to share some reference/code on this "Azure Cloud Service" that is able to wrap around wkhtmltopdf.exe ? Thanks in advance! 2PCoruscate
@TwoPea, I tried lots of different ways and other (static libraries), but as long as it uses wkhtmltopdf, it doesnt work (as far as I know). I could only think of 2 solutions, move my website to a VM (and not Azure Websites) or as I did, create a azure cloud service, that basically has the wkhtmltopdf in it. Creating a cloud(web) service is not THAT hard, as I thought it would be atleast.Emelun
@TwoPea, about the code, if you create a Cloud Service project, it will include some demo-code, (2 demo -methods). You can just rename them to something like 'GetPDF). Then you can change the implemenration to the exact same code as you have in your website that calls wkhtmltopdf. My Cloud Service method returns a byte[], which I then return back to the browser to download.Emelun
I am trying this but when creating a cloud service in VS2013 asked what type... web role, service web role, worker role, etc..which did you you use needacar?Luwian
web role if I recall correctly. I should note, the answer I initially gave no longer applies, I completely underestimated the costs of having a webservice running in azure. I found a cheaper solution which I will explain when I have more time soon.Emelun
@NeedACar, care to share your "cheaper solution"? I also don't want to pay for a web role just to have stupid wkhtmltopdf run on Azure... :-)Cato
Could you please share your cheaper solution with the community?Mastership
@Emelun can you please share your other solution? Stuck with same problem. Will try and create an Azure Cloud service for now, but unsure of how long i will stick with that solution.Stlouis
I'm the creator of Rotativa. I developed a service with the same Rotativa usage but you can use without having to worry about installing executables and having special permissions. It's online at rotativahq.comTraprock
@Mastership Microsoft has sponsored this wkhtmltopdf/image service wrapper on Azure and it is free to use under 200 requests per month: #25642951. This was the simplest drop-in replacement I found.Ruhl
@Headrail Just to know, is your comment still valid? As I got the thing to work on azure but it slowed everything else down until the PDF was generated. No other pages loaded while the pdf was being generated.Bivins
just revisiting Q1 2017 - still not supported by MS Azure WebApps, generated PDFs using EXE come out empty (not support for Qt/GDI?), TuesPechkin + TuesPechkin.Wkhtmltox don't execute because C++ Runtime is missing. Hopefully Azure WebApps eventually closes this gap!Yusuk
Y
34

Running wkhtmltopdf is now supported in Azure Web Apps - just make sure you are using Basic, Standard, or Premium App Service Plan. Consumption Service Plan and Free App Service Plan have a more limited sandbox and are NOT supported in my testing. MS confirms supportability of wkhtmltopdf in Azure Web Apps.

Hosting - App Service Plan

enter image description here

There are numerous wrappers available (TuesPechkin, Codaxy, etc.). Codaxy is an EXE wrapper in .NET while TuesPechkin is a .NET wrapper around C++ runtime using P/Invoke over wkhtmltox.dll.

Yusuk answered 9/3, 2017 at 21:38 Comment(4)
You Sir, are a Savior.Determinate
NOTE: Custom fonts are not supported using any wkhtmltopdf wrapperSuter
It didn't work on my node.js app, shall I define -command somewhere in code and what's the path??Enthusiast
Thanks You saved our day.Psalmody
E
8

Running wkhtmltopdf in Azure Websites in a simple way seems to be impossible, still. Static .NET wrappers like Pechkin wont work either in WebSites, there are very few alternatives that are 1. free and 2. dont use wkhtmltopdf or similar under the hood. Most alternatives like iTestSharp are not very advanced when it comes to html/css3/javascript reading (as I do some design changing with .js on page load).

I ended up creating a Azure Cloud Service, that runs wkhtmltopdf.exe without any issues. I send the html to the service, and get a byte[] in return. So far this seems to be working fine. Hope this helps others with similar problems.

Emelun answered 24/2, 2015 at 17:15 Comment(14)
FYI - the problem is not in running an exe vs using a .NET wrapper. Also there is a pretty good chance that alternatives to wkhtmltopdf won't work either. the root cause for wkhtmltopdf.exe not working is that most PDF converters use native Windows GDI+ API calls that are blocked in Azure Websites sandbox. Any tool using these API calls, which are responsible for rendering fonts and things like that, will be blocked. Creating an external service and using it just for the pdf generation is probably the best you can doHeadrail
Good comment, I figured that out later as well, my mind was fried after trying to make it work for 12hrs :)Emelun
Hi NeedACar, I am new to Azure and I recently hit the same issue. Is it possible for you to share some reference/code on this "Azure Cloud Service" that is able to wrap around wkhtmltopdf.exe ? Thanks in advance! 2PCoruscate
@TwoPea, I tried lots of different ways and other (static libraries), but as long as it uses wkhtmltopdf, it doesnt work (as far as I know). I could only think of 2 solutions, move my website to a VM (and not Azure Websites) or as I did, create a azure cloud service, that basically has the wkhtmltopdf in it. Creating a cloud(web) service is not THAT hard, as I thought it would be atleast.Emelun
@TwoPea, about the code, if you create a Cloud Service project, it will include some demo-code, (2 demo -methods). You can just rename them to something like 'GetPDF). Then you can change the implemenration to the exact same code as you have in your website that calls wkhtmltopdf. My Cloud Service method returns a byte[], which I then return back to the browser to download.Emelun
I am trying this but when creating a cloud service in VS2013 asked what type... web role, service web role, worker role, etc..which did you you use needacar?Luwian
web role if I recall correctly. I should note, the answer I initially gave no longer applies, I completely underestimated the costs of having a webservice running in azure. I found a cheaper solution which I will explain when I have more time soon.Emelun
@NeedACar, care to share your "cheaper solution"? I also don't want to pay for a web role just to have stupid wkhtmltopdf run on Azure... :-)Cato
Could you please share your cheaper solution with the community?Mastership
@Emelun can you please share your other solution? Stuck with same problem. Will try and create an Azure Cloud service for now, but unsure of how long i will stick with that solution.Stlouis
I'm the creator of Rotativa. I developed a service with the same Rotativa usage but you can use without having to worry about installing executables and having special permissions. It's online at rotativahq.comTraprock
@Mastership Microsoft has sponsored this wkhtmltopdf/image service wrapper on Azure and it is free to use under 200 requests per month: #25642951. This was the simplest drop-in replacement I found.Ruhl
@Headrail Just to know, is your comment still valid? As I got the thing to work on azure but it slowed everything else down until the PDF was generated. No other pages loaded while the pdf was being generated.Bivins
just revisiting Q1 2017 - still not supported by MS Azure WebApps, generated PDFs using EXE come out empty (not support for Qt/GDI?), TuesPechkin + TuesPechkin.Wkhtmltox don't execute because C++ Runtime is missing. Hopefully Azure WebApps eventually closes this gap!Yusuk

© 2022 - 2024 — McMap. All rights reserved.