asp.net mvc - RazorPDF save pdf file to server directory in MVC4 -
i assembling , displaying pdf using razorpdf in mvc4 , save pdf file file system @ same time return view.
the following line of code in controller action calling view:
return new pdfresult(claims, "pdf");
i able write pdf directory system changing code base of razorpdf render method. rendor method creates pdfwriter object associated response stream:
// associate output response stream var pdfwriter = pdfwriter.getinstance(document, viewcontext.httpcontext.response.outputstream); pdfwriter.closestream = false;
the solution create pdfwriter object associated filestream object illustrated below:
// create pdf file in directory system var filestream = new filestream(mypdffilepath, filemode.create); var pdfwriter2 = pdfwriter.getinstance(document, filestream);
i closed objects:
filestream.close(); pdfwriter.close(); pdfwriter2.close();
i had incorporate pdfresult , pdfview classes of razorpdf own project , alter code. reason because had encorporate calls email class sent pdf user.
the full render method displayed below:
public void render(viewcontext viewcontext, textwriter writer) { // generate view string var sb = new system.text.stringbuilder(); textwriter tw = new system.io.stringwriter(sb); myresult.view.render(viewcontext, tw); var resultcache = sb.tostring(); // detect itext (or html) format of response xmlparser parser; using (var reader = getxmlreader(resultcache)) { while (reader.read() && reader.nodetype != xmlnodetype.element) { // no-op } if (reader.nodetype == xmlnodetype.element && reader.name == "itext") parser = new xmlparser(); else parser = new htmlparser(); } // create document processing context var document = new document(); document.open(); // associate output response stream var pdfwriter = pdfwriter.getinstance(document, viewcontext.httpcontext.response.outputstream); pdfwriter.closestream = false; // create pdf file in directory system var filestream = new filestream(mypdffilepath, filemode.create); var pdfwriter2 = pdfwriter.getinstance(document, filestream); // close can being "success" before writing output // set content type viewcontext.httpcontext.response.contenttype = "application/pdf"; // parse memory through document output using (var reader = getxmlreader(resultcache)) { parser.go(document, reader); } filestream.close(); // send email claimant thread.sleep(100); if (file.exists(mypdffilepath)) { var subject = "pdf documents"; var body = config.getcontent(contentparams.claim_document_email_body_text); bool success; string errormessage; email.send(myemailaddress, subject, body, out success, out errormessage, mypdffilepath); } pdfwriter.close(); pdfwriter2.close(); }
it nice if capability somehow incorporated current razorpdf project.
Comments
Post a Comment