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+ }
0 commit comments