c# - Using tuespechkin with MVC project in Azure -
i can't manage pechkin or tuespechkin work on azure site.
whenever try access site hangs no error message (even customerrors off). there further setup i'm missing? works locally.
for 64 bit app i'm completing following steps:
- create new empty mvc app azure, make sure host in cloud selected
change app 64 bit
log onto azure portal , upgrade app basic hosting , change 64 bit
- install tuespechkin.wkhtmltox.win64 , tuespechkin nuget packages
add singleton class return iconverter
public class tuespechkinconverter { private static iconverter converter; public static iconverter converter { { if (converter == null) { converter = new threadsafeconverter( new pdftoolset( new win64embeddeddeployment( new tempfolderdeployment()))); } return converter; } } }
add home controller following code in index action:
var document = new htmltopdfdocument { globalsettings = { produceoutline = true, documenttitle = "pretty websites", papersize = paperkind.a4, // implicit conversion pechkinpapersize margins = { = 1.375, unit = unit.centimeters } }, objects = { new objectsettings { htmltext = "<h1>pretty websites</h1><p>this might take bit convert!</p>" }, new objectsettings { pageurl = "www.google.com" } } }; byte[] pdfbuf = tuespechkinconverter.converter.convert(document); return file(pdfbuf, "application/pdf", "downloadname.pdf");
as far know, can't make work in web app. however, there way can it: have create cloud service , add worker role it. tuespechkin installed in worker role.
the workflow following: cloud web app, access worker role(this thing possible configuring worker role host asp.net web api 2). worker role configure converter using tuespechkin , generate pdf. wrap pdf in web api response , send back. now, let's it...
to add cloud service (suppose have azure sdk installed), go visual studio -> right click solution -> add new project -> select cloud node -> azure cloud service -> after click ok select worker role , click ok.
your cloud service , worker role created. next thing configure worker role can host asp.net web api 2. configuration pretty straightforward, following this tutorial.
after have configured worker role host web api, have install tuespechkin.wkhtmltox.win64 , tuespechkin nuget packages.
your configuration should ready. create controller, in generate pdf: add new class in worker role extend apicontroller
:
public class pdfcontroller : apicontroller { }
add action our controller, return httpresponsemessage
object.
[httppost] public httpresponsemessage generatepdf(pdfviewmodel viewmodel) { }
here configure 2 objectsettings
, globalsettings
objects applied htmltopdfdocument
object. have 2 options.
you can generate pdf html text(maybe sent html of page in request) or directly page url.
var document = new htmltopdfdocument { globalsettings = { produceoutline = true, documenttitle = "pretty websites", papersize = paperkind.a4, // implicit conversion pechkinpapersize margins = { = 1.375, unit = unit.centimeters } }, objects = { new objectsettings { htmltext = "<h1>pretty websites</h1><p>this might take bit convert!</p>" }, new objectsettings { pageurl = "www.google.com" } } };
a nice thing when using page url, can use objectsettings
object post parameters:
var obj = new objectsettings(); obj.loadsettings.postitems.add ( new postitem() { name = "paramname", value = paramvalue } );
also, tuespechkin documentation converter should thread safe , should kept somewhere static, or singleton instance:
iconverter converter = new threadsafeconverter( new remotingtoolset<pdftoolset>( new win64embeddeddeployment( new tempfolderdeployment())));
finally wrap pdf in response content, set response content type application/pdf , add content-disposition header , that's it:
byte[] result = converter.convert(document); memorystream ms = new memorystream(result); response.statuscode = httpstatuscode.ok; response.content = new streamcontent(ms); response.content.headers.contenttype = new system.net.http.headers.mediatypeheadervalue("application/pdf"); response.content.headers.add("content-disposition", "attachment;filename=myfile.pdf"); return response;
Comments
Post a Comment