Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not compatible with last iTextSharp Version #1

Open
ftoyon opened this issue Apr 19, 2013 · 10 comments
Open

Not compatible with last iTextSharp Version #1

ftoyon opened this issue Apr 19, 2013 · 10 comments

Comments

@ftoyon
Copy link

ftoyon commented Apr 19, 2013

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

@XVincentX
Copy link

I agree to this issue too. Any change to make it work?

@XVincentX
Copy link

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);
    }
}

}

@ghost
Copy link

ghost commented Oct 20, 2013

Vincent. Nice work man. Why not do a pull request on this?

@XVincentX
Copy link

Done!

@Vyeche
Copy link

Vyeche commented Jun 4, 2014

@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/

@XVincentX
Copy link

@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

@Vyeche
Copy link

Vyeche commented Jun 4, 2014

Okay, I'll see if I can piece it together and get back to you. Thanks!

@germanger
Copy link

what happened to this?

@XVincentX
Copy link

Who knows...the author is simply dead.

@germanger
Copy link

@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)

@germanger
Copy link

Auto answering myself: https://github.com/andyhutch77/MvcRazorToPdf/ --this project uses the "new" iTextSharp.xmlparser

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants