Skip to content

Commit 1e1365f

Browse files
committed
Initial commit of dotnet library
1 parent 4bfeff2 commit 1e1365f

File tree

10 files changed

+438
-0
lines changed

10 files changed

+438
-0
lines changed

Api2Pdf.DotNet.sln

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.27004.2009
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Api2Pdf.DotNet", "Api2Pdf.DotNet\Api2Pdf.DotNet.csproj", "{2E91CC9C-D07D-4C07-83A5-74C79EFC3D83}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Examples.ConsoleApp", "Examples.ConsoleApp\Examples.ConsoleApp.csproj", "{E549F124-5451-4008-B055-17EB01635F8B}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{2E91CC9C-D07D-4C07-83A5-74C79EFC3D83}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{2E91CC9C-D07D-4C07-83A5-74C79EFC3D83}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{2E91CC9C-D07D-4C07-83A5-74C79EFC3D83}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{2E91CC9C-D07D-4C07-83A5-74C79EFC3D83}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{E549F124-5451-4008-B055-17EB01635F8B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{E549F124-5451-4008-B055-17EB01635F8B}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{E549F124-5451-4008-B055-17EB01635F8B}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{E549F124-5451-4008-B055-17EB01635F8B}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {38858B7D-62EF-4BDD-8D53-2051F869394D}
30+
EndGlobalSection
31+
EndGlobal
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
6+
<PackageId>Api2Pdf</PackageId>
7+
<Authors>Api2Pdf</Authors>
8+
<Company>Api2Pdf</Company>
9+
<Product>Api2Pdf</Product>
10+
<PackageLicenseUrl>https://github.com/Api2Pdf/api2pdf.dotnet/blob/master/LICENSE</PackageLicenseUrl>
11+
<PackageProjectUrl>https://github.com/Api2Pdf/api2pdf.dotnet/</PackageProjectUrl>
12+
<RepositoryUrl>https://github.com/Api2Pdf/api2pdf.dotnet/</RepositoryUrl>
13+
<PackageTags>pdf wkhtmtopdf headless chrome libreoffice</PackageTags>
14+
<Description>Wrapper for Api2Pdf.com - instantly generate PDFs from HTML, URLs or office documents. Also includes support for merge. Wrapper around wkhtmltopdf, headless chrome, and libre office.</Description>
15+
</PropertyGroup>
16+
17+
<ItemGroup>
18+
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
19+
</ItemGroup>
20+
21+
</Project>

Api2Pdf.DotNet/Api2Pdf.cs

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Net.Http;
4+
using Api2PdfLibrary.Models;
5+
using Api2PdfLibrary.Extensions;
6+
using System.Linq;
7+
using static Api2PdfLibrary.Api2Pdf;
8+
9+
namespace Api2PdfLibrary
10+
{
11+
public class Api2Pdf
12+
{
13+
public const string API_BASE_URL = "https://v2018.api2pdf.com/";
14+
public static HttpClient _httpClient;
15+
16+
private string _apiKey;
17+
public LibreOfficeHandler LibreOffice;
18+
public HeadlessChromeHandler HeadlessChrome;
19+
public WkHtmlToPdfHandler WkHtmlToPdf;
20+
21+
22+
23+
public class Api2PdfResponse
24+
{
25+
public string Pdf { get; set; }
26+
public double MbIn { get; set; }
27+
public double MbOut { get; set; }
28+
public double Cost { get; set; }
29+
public bool Success { get; set; }
30+
public string Error { get; set; }
31+
32+
public void SavePdf(string localPath)
33+
{
34+
var wc = new System.Net.WebClient();
35+
wc.DownloadFile(Pdf, localPath);
36+
}
37+
38+
public byte[] GetPdfBytes()
39+
{
40+
var wc = new System.Net.WebClient();
41+
return wc.DownloadData(Pdf);
42+
}
43+
}
44+
45+
public Api2Pdf(string apiKey, string tag = null)
46+
{
47+
_apiKey = apiKey;
48+
LibreOffice = new LibreOfficeHandler(apiKey);
49+
HeadlessChrome = new HeadlessChromeHandler(apiKey);
50+
WkHtmlToPdf = new WkHtmlToPdfHandler(apiKey);
51+
52+
if (_httpClient == null)
53+
{
54+
_httpClient = new HttpClient();
55+
_httpClient.DefaultRequestHeaders.Add("Authorization", apiKey);
56+
57+
if(!string.IsNullOrWhiteSpace(tag))
58+
_httpClient.DefaultRequestHeaders.Add("Tag", tag);
59+
}
60+
}
61+
62+
public Api2PdfResponse Merge(IEnumerable<string> pdfUrls, bool inline = false, string outputFileName = null)
63+
{
64+
var mergeRequest = new MergeRequest
65+
{
66+
Urls = pdfUrls.ToArray(),
67+
FileName = outputFileName,
68+
InlinePdf = inline
69+
};
70+
71+
72+
return _httpClient.PostPdfRequest<Api2PdfResponse>($"{API_BASE_URL}/merge", mergeRequest);
73+
}
74+
}
75+
76+
public class LibreOfficeHandler
77+
{
78+
private string _apiKey;
79+
public LibreOfficeHandler(string apiKey)
80+
{
81+
_apiKey = apiKey;
82+
}
83+
84+
public Api2PdfResponse Convert(string url, bool inline = false, string outputFileName = null)
85+
{
86+
var libreRequest = new LibreOfficeConvertRequest
87+
{
88+
FileName = outputFileName,
89+
InlinePdf = inline,
90+
Url = url
91+
};
92+
93+
return _httpClient.PostPdfRequest<Api2PdfResponse>($"{API_BASE_URL}/libreoffice/convert", libreRequest);
94+
}
95+
}
96+
97+
public class WkHtmlToPdfHandler
98+
{
99+
private string _apiKey;
100+
public WkHtmlToPdfHandler(string apiKey)
101+
{
102+
_apiKey = apiKey;
103+
}
104+
105+
public Api2PdfResponse FromHtml(string html, bool inline = false, string outputFileName = null, params KeyValuePair<string, string>[] options)
106+
{
107+
var wkRequest = new WkHtmlToPdfHtmlRequest
108+
{
109+
FileName = outputFileName,
110+
InlinePdf = inline,
111+
Html = html,
112+
Options = new Dictionary<string, string>()
113+
};
114+
115+
if(options != null)
116+
{
117+
foreach (var o in options)
118+
wkRequest.Options[o.Key] = o.Value;
119+
}
120+
121+
return _httpClient.PostPdfRequest<Api2PdfResponse>($"{API_BASE_URL}/wkhtmltopdf/html", wkRequest);
122+
}
123+
124+
public Api2PdfResponse FromUrl(string url, bool inline = false, string outputFileName = null, params KeyValuePair<string, string>[] options)
125+
{
126+
var wkRequest = new WkHtmlToPdfUrlRequest
127+
{
128+
FileName = outputFileName,
129+
InlinePdf = inline,
130+
Url = url,
131+
Options = new Dictionary<string, string>()
132+
};
133+
134+
if (options != null)
135+
{
136+
foreach (var o in options)
137+
wkRequest.Options[o.Key] = o.Value;
138+
}
139+
140+
return _httpClient.PostPdfRequest<Api2PdfResponse>($"{API_BASE_URL}/wkhtmltopdf/url", wkRequest);
141+
}
142+
}
143+
144+
public class HeadlessChromeHandler
145+
{
146+
private string _apiKey;
147+
public HeadlessChromeHandler(string apiKey)
148+
{
149+
_apiKey = apiKey;
150+
}
151+
152+
public Api2PdfResponse FromHtml(string html, bool inline = false, string outputFileName = null, params KeyValuePair<string, string>[] options)
153+
{
154+
var chromeRequest = new ChromeHtmlRequest
155+
{
156+
FileName = outputFileName,
157+
InlinePdf = inline,
158+
Html = html,
159+
Options = new Dictionary<string, string>()
160+
};
161+
162+
if (options != null)
163+
{
164+
foreach (var o in options)
165+
chromeRequest.Options[o.Key] = o.Value;
166+
}
167+
168+
return _httpClient.PostPdfRequest<Api2PdfResponse>($"{API_BASE_URL}/chrome/html", chromeRequest);
169+
}
170+
171+
public Api2PdfResponse FromUrl(string url, bool inline = false, string outputFileName = null, params KeyValuePair<string, string>[] options)
172+
{
173+
var chromeRequest = new ChromeUrlRequest
174+
{
175+
FileName = outputFileName,
176+
InlinePdf = inline,
177+
Url = url,
178+
Options = new Dictionary<string, string>()
179+
};
180+
181+
if (options != null)
182+
{
183+
foreach (var o in options)
184+
chromeRequest.Options[o.Key] = o.Value;
185+
}
186+
187+
return _httpClient.PostPdfRequest<Api2PdfResponse>($"{API_BASE_URL}/chrome/url", chromeRequest);
188+
}
189+
}
190+
}

Api2Pdf.DotNet/Extensions.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using System.Net.Http;
5+
using Newtonsoft.Json;
6+
using Newtonsoft.Json.Serialization;
7+
8+
namespace Api2PdfLibrary.Extensions
9+
{
10+
public static class HttpClientExtensions
11+
{
12+
public static T PostPdfRequest<T>(this HttpClient httpClient, string url, object obj)
13+
{
14+
var serializerSettings = new JsonSerializerSettings();
15+
serializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
16+
17+
var content = new StringContent(JsonConvert.SerializeObject(obj, serializerSettings));
18+
return JsonConvert.DeserializeObject<T>(httpClient.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result);
19+
}
20+
}
21+
}

Api2Pdf.DotNet/Models.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Api2PdfLibrary.Models
6+
{
7+
public abstract class PdfRequestBase
8+
{
9+
public bool InlinePdf { get; set; }
10+
public string FileName { get; set; }
11+
}
12+
13+
public class WkHtmlToPdfUrlRequest : PdfRequestBase
14+
{
15+
public string Url { get; set; }
16+
17+
public Dictionary<string, string> Options { get; set; }
18+
}
19+
20+
public class WkHtmlToPdfHtmlRequest : PdfRequestBase
21+
{
22+
public string Html { get; set; }
23+
public Dictionary<string, string> Options { get; set; }
24+
}
25+
26+
public class ChromeUrlRequest : PdfRequestBase
27+
{
28+
public string Url { get; set; }
29+
public Dictionary<string, string> Options { get; set; }
30+
}
31+
32+
public class ChromeHtmlRequest : PdfRequestBase
33+
{
34+
public string Html { get; set; }
35+
public Dictionary<string, string> Options { get; set; }
36+
}
37+
38+
public class LibreOfficeConvertRequest : PdfRequestBase
39+
{
40+
public string Url { get; set; }
41+
}
42+
43+
public class MergeRequest : PdfRequestBase
44+
{
45+
public string[] Urls { get; set; }
46+
}
47+
}

Examples.ConsoleApp/App.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
5+
</startup>
6+
</configuration>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{E549F124-5451-4008-B055-17EB01635F8B}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>Examples.ConsoleApp</RootNamespace>
10+
<AssemblyName>Examples.ConsoleApp</AssemblyName>
11+
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<PlatformTarget>AnyCPU</PlatformTarget>
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<PlatformTarget>AnyCPU</PlatformTarget>
27+
<DebugType>pdbonly</DebugType>
28+
<Optimize>true</Optimize>
29+
<OutputPath>bin\Release\</OutputPath>
30+
<DefineConstants>TRACE</DefineConstants>
31+
<ErrorReport>prompt</ErrorReport>
32+
<WarningLevel>4</WarningLevel>
33+
</PropertyGroup>
34+
<ItemGroup>
35+
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
36+
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
37+
</Reference>
38+
<Reference Include="System" />
39+
<Reference Include="System.Core" />
40+
<Reference Include="System.Xml.Linq" />
41+
<Reference Include="System.Data.DataSetExtensions" />
42+
<Reference Include="Microsoft.CSharp" />
43+
<Reference Include="System.Data" />
44+
<Reference Include="System.Net.Http" />
45+
<Reference Include="System.Xml" />
46+
</ItemGroup>
47+
<ItemGroup>
48+
<Compile Include="Program.cs" />
49+
<Compile Include="Properties\AssemblyInfo.cs" />
50+
</ItemGroup>
51+
<ItemGroup>
52+
<None Include="App.config" />
53+
<None Include="packages.config" />
54+
</ItemGroup>
55+
<ItemGroup>
56+
<ProjectReference Include="..\Api2Pdf.DotNet\Api2Pdf.DotNet.csproj">
57+
<Project>{2e91cc9c-d07d-4c07-83a5-74c79efc3d83}</Project>
58+
<Name>Api2Pdf.DotNet</Name>
59+
</ProjectReference>
60+
</ItemGroup>
61+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
62+
</Project>

0 commit comments

Comments
 (0)