Open ftoyon opened 11 years ago
I agree to this issue too. Any change to make it work?
This code will work
using iTextSharp.text; using iTextSharp.text.html.simpleparser; using iTextSharp.text.pdf; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Web; using System.Web.Mvc; using System.Xml;
namespace RazorPDF { public class PdfView : IView, IViewEngine { private readonly ViewEngineResult _result;
public PdfView(ViewEngineResult result)
{
_result = result;
}
public void Render(ViewContext viewContext, TextWriter writer)
{
// generate view into string
var sb = new System.Text.StringBuilder();
TextWriter tw = new System.IO.StringWriter(sb);
_result.View.Render(viewContext, tw);
var resultCache = sb.ToString();
var ms = new MemoryStream();
var document = new Document();
var pdfWriter = PdfWriter.GetInstance(document, ms);
var worker = new HTMLWorker(document);
document.Open();
worker.StartDocument();
pdfWriter.CloseStream = false;
worker.Parse(new StringReader(resultCache));
worker.EndDocument();
worker.Close();
document.CloseDocument();
document.Close();
// this is as close as we can get to being "success" before writing output
// so set the content type now
viewContext.HttpContext.Response.ContentType = "application/pdf";
pdfWriter.Flush();
pdfWriter.Close();
viewContext.HttpContext.Response.BinaryWrite(ms.ToArray());
}
private static XmlTextReader GetXmlReader(string source)
{
byte[] byteArray = Encoding.UTF8.GetBytes(source);
MemoryStream stream = new MemoryStream(byteArray);
var xtr = new XmlTextReader(stream);
xtr.WhitespaceHandling = WhitespaceHandling.None; // Helps iTextSharp parse
return xtr;
}
public ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)
{
throw new System.NotImplementedException();
}
public ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
{
throw new System.NotImplementedException();
}
public void ReleaseView(ControllerContext controllerContext, IView view)
{
_result.ViewEngine.ReleaseView(controllerContext, _result.View);
}
}
public class PdfResult : ViewResult
{
//Constructors
public PdfResult(object model, string name)
{
ViewData = new ViewDataDictionary(model);
ViewName = name;
}
public PdfResult()
: this(new ViewDataDictionary(), "Pdf")
{
}
public PdfResult(object model)
: this(model, "Pdf")
{
}
//Override FindView to load PdfView
protected override ViewEngineResult FindView(ControllerContext context)
{
var result = base.FindView(context);
if (result.View == null)
return result;
var pdfView = new PdfView(result);
return new ViewEngineResult(pdfView, pdfView);
}
}
}
Vincent. Nice work man. Why not do a pull request on this?
Done!
@XVincentX I'm trying to upgrade some of the methods in PdfView.cs file to use the new 5.0 methods, but I can't seem to find the equivalents. I'm looking at there new APi: http://api.itextpdf.com/itext/ and I noticed that they have done something with the Go method of the XmlParser. The HtmlParser, is not there anymore or I'm not sure what the equivalent for that is. Do you mind pointing out which should be used?
I was also using the older api: http://www.afterlogic.com/mailbee-net/docs-itextsharp/
@Vyeche Honestly I remember few things about this contribution, but I'm not going to leave you alone. What I'm asking to you is to create a sample project with the issue (due to my really few time) and upload it. I promise I will give it a look. Thank you, V.Chianese
Okay, I'll see if I can piece it together and get back to you. Thanks!
what happened to this?
Who knows...the author is simply dead.
@XVincentX , do you know any alternative to RazorPDF ? Rotativa is great but uses wkhtmltopdf.exe, which is not possible to use in a shared hosting (running exes is not allowed)
Auto answering myself: https://github.com/andyhutch77/MvcRazorToPdf/ --this project uses the "new" iTextSharp.xmlparser
Hi,
After update iTextSharp Nuget package to the last 5.3.3 version the parse failed because iTextSharp.text.xml.XMLParse can not be found.
Thank's for work.
Best regard's