Skip to content

Commit 9570848

Browse files
committed
Added example for using library in web server.
1 parent 17d6134 commit 9570848

10 files changed

+9691
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Mvc;
6+
using DinkToPdf.Contracts;
7+
using System.IO;
8+
9+
namespace DinkToPdf.TestWebServer.Controllers
10+
{
11+
[Route("api/[controller]")]
12+
public class ConvertController : Controller
13+
{
14+
private IConverter _converter;
15+
16+
public ConvertController(IConverter converter)
17+
{
18+
_converter = converter;
19+
}
20+
21+
// GET api/values
22+
[HttpGet]
23+
public IActionResult Get()
24+
{
25+
var doc = new HtmlToPdfDocument()
26+
{
27+
GlobalSettings = {
28+
PaperSize = PaperKind.A3,
29+
Orientation = Orientation.Landscape,
30+
},
31+
32+
Objects = {
33+
new ObjectSettings()
34+
{
35+
Page = "http://google.com/",
36+
},
37+
new ObjectSettings()
38+
{
39+
Page = "https://github.com/",
40+
41+
}
42+
}
43+
};
44+
45+
byte[] pdf = _converter.Convert(doc);
46+
47+
48+
return new FileContentResult(pdf, "application/pdf");
49+
}
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
5+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
6+
</PropertyGroup>
7+
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
8+
<PropertyGroup Label="Globals">
9+
<ProjectGuid>292a280a-24fd-41d9-867c-058e6ee6028f</ProjectGuid>
10+
<RootNamespace>DinkToPdf.TestWebServer</RootNamespace>
11+
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
12+
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
13+
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
14+
</PropertyGroup>
15+
<PropertyGroup>
16+
<SchemaVersion>2.0</SchemaVersion>
17+
</PropertyGroup>
18+
<Import Project="$(VSToolsPath)\DotNet.Web\Microsoft.DotNet.Web.targets" Condition="'$(VSToolsPath)' != ''" />
19+
</Project>

DinkToPdf.TestWebServer/Program.cs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Hosting;
7+
using Microsoft.AspNetCore.Builder;
8+
9+
namespace DinkToPdf.TestWebServer
10+
{
11+
public class Program
12+
{
13+
public static void Main(string[] args)
14+
{
15+
var host = new WebHostBuilder()
16+
.UseKestrel()
17+
.UseContentRoot(Directory.GetCurrentDirectory())
18+
.UseStartup<Startup>()
19+
.Build();
20+
21+
host.Run();
22+
}
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
5+
"iisExpress": {
6+
"applicationUrl": "http://localhost:50554/",
7+
"sslPort": 0
8+
}
9+
},
10+
"profiles": {
11+
"IIS Express": {
12+
"commandName": "IISExpress",
13+
"launchBrowser": true,
14+
"launchUrl": "api/values",
15+
"environmentVariables": {
16+
"ASPNETCORE_ENVIRONMENT": "Development"
17+
}
18+
},
19+
"DinkToPdf.TestWebServer": {
20+
"commandName": "Project",
21+
"launchBrowser": true,
22+
"launchUrl": "http://localhost:5000/api/values",
23+
"environmentVariables": {
24+
"ASPNETCORE_ENVIRONMENT": "Development"
25+
}
26+
}
27+
}
28+
}

DinkToPdf.TestWebServer/Startup.cs

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Builder;
6+
using Microsoft.AspNetCore.Hosting;
7+
using Microsoft.Extensions.Configuration;
8+
using Microsoft.Extensions.DependencyInjection;
9+
using Microsoft.Extensions.Logging;
10+
using DinkToPdf.Contracts;
11+
12+
namespace DinkToPdf.TestWebServer
13+
{
14+
//************************************* IMPORTANT ***********************************
15+
// Copy native library to root folder of your project. From there .NET Core loads native library when native method is called with P/Invoke. You can find latest version of native library https://github.com/rdvojmoc/DinkToPdf/tree/master/v0.12.4. Select appropriate library for your OS and platform (64 or 32 bit).
16+
public class Startup
17+
{
18+
public Startup(IHostingEnvironment env)
19+
{
20+
var builder = new ConfigurationBuilder()
21+
.SetBasePath(env.ContentRootPath)
22+
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
23+
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
24+
25+
if (env.IsEnvironment("Development"))
26+
{
27+
// This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately.
28+
builder.AddApplicationInsightsSettings(developerMode: true);
29+
}
30+
31+
builder.AddEnvironmentVariables();
32+
Configuration = builder.Build();
33+
}
34+
35+
public IConfigurationRoot Configuration { get; }
36+
37+
// This method gets called by the runtime. Use this method to add services to the container
38+
public void ConfigureServices(IServiceCollection services)
39+
{
40+
// Add converter to DI
41+
services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));
42+
// Add framework services.
43+
services.AddApplicationInsightsTelemetry(Configuration);
44+
45+
services.AddMvc();
46+
}
47+
48+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline
49+
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
50+
{
51+
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
52+
loggerFactory.AddDebug();
53+
54+
app.UseApplicationInsightsRequestTelemetry();
55+
56+
app.UseApplicationInsightsExceptionTelemetry();
57+
58+
app.UseMvc();
59+
}
60+
}
61+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"Logging": {
3+
"IncludeScopes": false,
4+
"LogLevel": {
5+
"Default": "Debug",
6+
"System": "Information",
7+
"Microsoft": "Information"
8+
}
9+
}
10+
}

DinkToPdf.TestWebServer/project.json

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{
2+
"dependencies": {
3+
"Microsoft.NETCore.App": {
4+
"version": "1.0.1",
5+
"type": "platform"
6+
},
7+
"Microsoft.ApplicationInsights.AspNetCore": "1.0.0",
8+
"Microsoft.AspNetCore.Mvc": "1.0.1",
9+
"Microsoft.AspNetCore.Routing": "1.0.1",
10+
"Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
11+
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
12+
"Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
13+
"Microsoft.Extensions.Configuration.Json": "1.0.0",
14+
"Microsoft.Extensions.Logging": "1.0.0",
15+
"Microsoft.Extensions.Logging.Console": "1.0.0",
16+
"Microsoft.Extensions.Logging.Debug": "1.0.0",
17+
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
18+
"Autofac": "4.0.0-rc3-293",
19+
"Autofac.Extensions.DependencyInjection": "4.0.0-rc3-280",
20+
"DinkToPdf": "1.0.7"
21+
},
22+
23+
"tools": {
24+
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
25+
},
26+
27+
"frameworks": {
28+
"netcoreapp1.0": {
29+
"imports": [
30+
"dotnet5.6",
31+
"portable-net45+win8"
32+
]
33+
}
34+
},
35+
36+
"buildOptions": {
37+
"emitEntryPoint": true,
38+
"preserveCompilationContext": true
39+
},
40+
41+
"runtimeOptions": {
42+
"configProperties": {
43+
"System.GC.Server": true
44+
}
45+
},
46+
47+
"publishOptions": {
48+
"include": [
49+
"wwwroot",
50+
"**/*.cshtml",
51+
"appsettings.json",
52+
"web.config"
53+
]
54+
},
55+
56+
"scripts": {
57+
58+
},
59+
60+
"commands": {
61+
"Kestrel": "Microsoft.AspNetCore.Server.Kestrel"
62+
}
63+
}

0 commit comments

Comments
 (0)