diff --git a/RazorPDF/PdfResult.cs b/RazorPDF/PdfResult.cs index 8ca136e..b8ad18b 100644 --- a/RazorPDF/PdfResult.cs +++ b/RazorPDF/PdfResult.cs @@ -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); + } + } +} diff --git a/RazorPDF/PdfView.cs b/RazorPDF/PdfView.cs index e875f75..dd22ae3 100644 --- a/RazorPDF/PdfView.cs +++ b/RazorPDF/PdfView.cs @@ -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); + } + } +} diff --git a/RazorPDF/Properties/AssemblyInfo.cs b/RazorPDF/Properties/AssemblyInfo.cs index a686879..5beaf50 100644 --- a/RazorPDF/Properties/AssemblyInfo.cs +++ b/RazorPDF/Properties/AssemblyInfo.cs @@ -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")] @@ -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")] diff --git a/RazorPDF/RazorPDF.csproj b/RazorPDF/RazorPDF.csproj index fdd67b1..9a37b59 100644 --- a/RazorPDF/RazorPDF.csproj +++ b/RazorPDF/RazorPDF.csproj @@ -1,69 +1,96 @@ - - - - Debug - AnyCPU - 8.0.30703 - 2.0 - {0EEB1093-C4B8-4830-BC01-2C56E618702E} - Library - Properties - RazorPDF - RazorPDF - v4.0 - 512 - ..\ - true - - - - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - false - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - false - - - - ..\packages\iTextSharp.4.1.2\lib\itextsharp.dll - - - - - - - - - - - - - - - - - - - - - + + + + Debug + AnyCPU + 8.0.30703 + 2.0 + {0EEB1093-C4B8-4830-BC01-2C56E618702E} + Library + Properties + RazorPDF + RazorPDF + v4.5.1 + 512 + ..\ + true + + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + false + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + false + + + + ..\packages\iTextSharp.4.1.2\lib\itextsharp.dll + + + True + ..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll + + + + + + False + ..\packages\Microsoft.AspNet.WebPages.3.1.1\lib\net45\System.Web.Helpers.dll + + + False + ..\packages\Microsoft.AspNet.Mvc.5.1.1\lib\net45\System.Web.Mvc.dll + + + False + ..\packages\Microsoft.AspNet.Razor.3.1.1\lib\net45\System.Web.Razor.dll + + + False + ..\packages\Microsoft.AspNet.WebPages.3.1.1\lib\net45\System.Web.WebPages.dll + + + False + ..\packages\Microsoft.AspNet.WebPages.3.1.1\lib\net45\System.Web.WebPages.Deployment.dll + + + False + ..\packages\Microsoft.AspNet.WebPages.3.1.1\lib\net45\System.Web.WebPages.Razor.dll + + + + + + + + + + + + + + + + + + --> \ No newline at end of file diff --git a/RazorPDF/RazorPDF.nuspec b/RazorPDF/RazorPDF.nuspec new file mode 100644 index 0000000..5122b4c --- /dev/null +++ b/RazorPDF/RazorPDF.nuspec @@ -0,0 +1,18 @@ + + + + $id$ + $version$ + $title$ + $author$ + $author$ + http://www.apache.org/licenses/ + https://github.com/gverduci/RazorPDF + http://nyveldt.com/ + false + $description$ + Added support to itextsharp page events. + Copyright 2014 + Pdf iTextSharp + + \ No newline at end of file diff --git a/RazorPDF/packages.config b/RazorPDF/packages.config index a82249e..7f01207 100644 --- a/RazorPDF/packages.config +++ b/RazorPDF/packages.config @@ -1,4 +1,8 @@ - - - + + + + + + + \ No newline at end of file