Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 58 additions & 50 deletions RazorPDF/PdfResult.cs
Original file line number Diff line number Diff line change
@@ -1,50 +1,58 @@
// Copyright 2012 Al Nyveldt - http://nyveldt.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace RazorPDF
{
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);
}
}
}
using iTextSharp.text.pdf;
// Copyright 2012 Al Nyveldt - http://nyveldt.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace RazorPDF
{
public class PdfResult : ViewResult
{
PdfPageEventHelper pageEventHelper = null;
//Constructors
public PdfResult(object model, string name, PdfPageEventHelper pageEventHelper)
{
ViewData = new ViewDataDictionary(model);
ViewName = name;
this.pageEventHelper = pageEventHelper;
}
public PdfResult(object model, string name)
: this(model, name, null)
{

}
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, pageEventHelper);
return new ViewEngineResult(pdfView, pdfView);
}
}
}
224 changes: 116 additions & 108 deletions RazorPDF/PdfView.cs
Original file line number Diff line number Diff line change
@@ -1,108 +1,116 @@
// Copyright 2012 Al Nyveldt - http://nyveldt.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Xml;
using System.Xml.Linq;
using iTextSharp.text;
using iTextSharp.text.html;
using iTextSharp.text.pdf;
using iTextSharp.text.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();

// 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 a document processing context
var document = new Document();
document.Open();

// associate output with response stream
var pdfWriter = PdfWriter.GetInstance(document, viewContext.HttpContext.Response.OutputStream);
pdfWriter.CloseStream = false;

// 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";

// parse memory through document into output
using (var reader = GetXmlReader(resultCache))
{
parser.Go(document, reader);
}

pdfWriter.Close();
}

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);
}
}
}
// Copyright 2012 Al Nyveldt - http://nyveldt.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Xml;
using System.Xml.Linq;
using iTextSharp.text;
using iTextSharp.text.html;
using iTextSharp.text.pdf;
using iTextSharp.text.xml;

namespace RazorPDF
{
public class PdfView : IView, IViewEngine
{
private readonly ViewEngineResult _result;
private PdfPageEventHelper pageEventHelper = null;
public PdfView(ViewEngineResult result)
: this(result, null)
{
}

public PdfView(ViewEngineResult result, PdfPageEventHelper pageEventHelper)
{
_result = result;
this.pageEventHelper = pageEventHelper;
}

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

// 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 a document processing context
var document = new Document();
document.Open();

// associate output with response stream
var pdfWriter = PdfWriter.GetInstance(document, viewContext.HttpContext.Response.OutputStream);
if (pageEventHelper != null)
pdfWriter.PageEvent = pageEventHelper;
pdfWriter.CloseStream = false;

// 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";

// parse memory through document into output
using (var reader = GetXmlReader(resultCache))
{
parser.Go(document, reader);
}

pdfWriter.Close();
}

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);
}
}
}
8 changes: 4 additions & 4 deletions RazorPDF/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("RazorPDF")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyTitle("RazorPDF")]
[assembly: AssemblyDescription("RazorPDF is a simple package that allow you to use a Razor View to generate a PDF.")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Al Nyveldt")]
[assembly: AssemblyProduct("RazorPDF")]
Expand All @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.0.0.1")]
[assembly: AssemblyFileVersion("1.0.0.1")]
Loading