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
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
# mstest test results
TestResults

# Ignore Visual Studio and Visual Studo Code settings
.vs/
.vscode/

# ignore the *.lock files
*.lock.json

## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.

Expand Down
6 changes: 0 additions & 6 deletions .nuget/NuGet.Config

This file was deleted.

Binary file removed .nuget/NuGet.exe
Binary file not shown.
150 changes: 0 additions & 150 deletions .nuget/NuGet.targets

This file was deleted.

47 changes: 47 additions & 0 deletions ControllerExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
namespace RazorPDFCore
{
public class Controller : Microsoft.AspNetCore.Mvc.Controller
{
public PdfResult ViewPdf(object model, string fileName)
{
ViewData.Model = model;

return new PdfResult()
{
FileName = fileName,
TempData = TempData,
ViewData = ViewData
};
}

public PdfResult ViewPdf(object model, string fileName, bool download = false, iTextSharp.text.Rectangle pageSize = null)
{
ViewData.Model = model;

return new PdfResult()
{
FileName = fileName,
TempData = TempData,
ViewData = ViewData,
Download = download
};
}

public PdfResult ViewPdf(object model, string fileName, string viewName, bool download = false, iTextSharp.text.Rectangle pageSize = null)
{
ViewData.Model = model;

if (pageSize == null) pageSize = iTextSharp.text.PageSize.A4;

return new PdfResult()
{
ViewName = viewName,
FileName = fileName,
TempData = TempData,
ViewData = ViewData,
Download = download,
PageSize = pageSize
};
}
}
}
90 changes: 90 additions & 0 deletions PdfResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright 2016 Al Nyveldt - http://nyveldt.com, Ole Koeckemann <ole.k@web.de>
//
// 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 iTextSharp.text;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewEngines;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Mvc.ViewFeatures.Internal;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.IO;
using System.Threading.Tasks;
using System.Xml;

namespace RazorPDFCore
{
public class PdfResult : ActionResult
{
/// <summary>
/// Gets or sets the Content-Type header for the response.
/// </summary>
public string ContentType { get; set; }
public string ContentDisposition { get; private set; }
/// <summary>
/// Gets the view data model.
/// </summary>
public object Model => ViewData?.Model;

public int StatusCode { get { return 200; } }

public ViewDataDictionary ViewData{ get; set; }

public ITempDataDictionary TempData { get; set; }

public string ViewName { get; set; }

public string FileName { get; set; }
public bool Download { get; set; }

public Rectangle PageSize { get; set; } = iTextSharp.text.PageSize.A4;

public IViewEngine ViewEngine { get; set; }


//Constructors
public PdfResult()
{
ContentType = "application/pdf";
}

public async override Task ExecuteResultAsync(ActionContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}

if(Download)
ContentDisposition = $"attachment; filename=\"{FileName}\"";
else
ContentDisposition = $"inline; filename=\"{FileName}\"";

var services = context.HttpContext.RequestServices;
var executor = services.GetRequiredService<PdfResultExecutor>();

var result = executor.FindView(context, this);
result.EnsureSuccessful(originalLocations: null);

var view = result.View;
using (view as IDisposable)
{
await executor.ExecuteAsync(context, view, this);
}
}
}
}
Loading