├── Properties
└── AssemblyInfo.cs
├── appveyor.yml
├── LICENSE
├── ImageProcessor.Plugins.Pdf.sln
├── README.md
├── ImageProcessor.Plugins.Pdf.csproj
├── PdfFormat.cs
├── PdfRasterizer.cs
└── .gitignore
/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // Setting ComVisible to false makes the types in this assembly not visible
6 | // to COM components. If you need to access a type in this assembly from
7 | // COM, set the ComVisible attribute to true on that type.
8 | [assembly: ComVisible(false)]
9 |
10 | // The following GUID is for the ID of the typelib if this project is exposed to COM
11 | [assembly: Guid("ecb0f4b0-0e24-4ca5-8bdf-a6de25f59171")]
12 |
--------------------------------------------------------------------------------
/appveyor.yml:
--------------------------------------------------------------------------------
1 | os: Visual Studio 2017
2 |
3 | # version format
4 | version: 1.3.{build}
5 |
6 | cache:
7 | - src\packages -> **\packages.config # preserve "packages" directory in the root of build folder but will reset it if packages.config is modified
8 |
9 | build:
10 | project: ImageProcessor.Plugins.Pdf.csproj
11 |
12 | before_build:
13 | - nuget restore
14 |
15 | artifacts:
16 | - path: '**\*.nupkg' # find all NuGet packages recursively
17 | name: MyApp
18 |
19 | deploy:
20 | - provider: NuGet
21 | api_key:
22 | secure: hd63fmfN87wpPr16CMeB3I4jj/+mj0oHJxUIaQb7t61spQKNJfwxmfp059BKs6DX
23 | skip_symbols: false
24 | artifact: MyApp
25 | - provider: GitHub
26 | artifact: MyApp
27 | auth_token:
28 | secure: OFvuxAZ0fV+i5iZDD4bfNLyy8H4fWQpJUOyLX2OybtSFtgYYUwm06qADOy950/Ui
29 |
30 | dotnet_csproj:
31 | patch: true
32 | file: '*.csproj'
33 | version: '{version}'
34 | package_version: '{version}'
35 |
36 | configuration:
37 | - Release
38 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017 Damiaan
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/ImageProcessor.Plugins.Pdf.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.26403.3
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ImageProcessor.Plugins.Pdf", "ImageProcessor.Plugins.Pdf.csproj", "{1CFB1BC1-86B3-415A-9D26-973C5F84FA07}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {1CFB1BC1-86B3-415A-9D26-973C5F84FA07}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {1CFB1BC1-86B3-415A-9D26-973C5F84FA07}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {1CFB1BC1-86B3-415A-9D26-973C5F84FA07}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {1CFB1BC1-86B3-415A-9D26-973C5F84FA07}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | GlobalSection(ExtensibilityGlobals) = postSolution
23 | SolutionGuid = {D43ABF6A-2450-4CD7-9FCA-0E5A228929FC}
24 | EndGlobalSection
25 | EndGlobal
26 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | [](https://ci.appveyor.com/project/dampee/imageprocessor-plugins-pdf)
2 | [](https://www.nuget.org/packages/ImageProcessor.Plugins.Pdf/)
3 |
4 | # Pdf fileformat for ImageProcessor
5 |
6 | This is a plugin for ImageProcessorThis project is a plugin for [ImageProcessor](http://imageprocessor.org/).
7 | *It adds PDF as a supported file format*. This gives you the ability to preview the first page of a PDF file on your web page.
8 |
9 | ## Installation using Nuget
10 |
11 | Install-Package ImageProcessor.Plugins.PDF
12 |
13 | ## GhostScript
14 | We use [GhostScript.Net](https://github.com/jhabjan/Ghostscript.NET) as a managed wrapper for [GhostScript](https://ghostscript.com/).
15 | The good thing is, you don't need to install ghostscript on your machine.
16 | You can just copy (one or both) `gsdll32.dll` and `gsdll64.dll` in the /bin directory.
17 | **These files are not provided in this packages and must be added manually.**
18 |
19 | I read that Ghostscript does not handle requests from multiple processes.
20 | When running on a webserver, be sure to run the website in his own applicationpool.
21 |
22 | ## Configuration
23 | Just add `/myfile.pdf?format=jpg&width=500` to get the first page as an image.
24 |
25 | ## running locally on IIS express
26 | If you run locally, and you have the 64 bit version of ghostscript installed,
27 | don't forget to [activate 64 bit of IIS express](https://visualstudio.uservoice.com/forums/121579-visual-studio-ide/suggestions/3254745-allow-for-iis-express-64-bit-to-run-from-visual-st).
28 |
29 |
30 |
--------------------------------------------------------------------------------
/ImageProcessor.Plugins.Pdf.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | net45;net46;net47
4 | ImageProcessor.Plugins.Pdf
5 | ImageProcessor.Plugins.Pdf
6 | A plugin for ImageProcessor adding PDF as a recognized input format. This allows you to easily show thumbnails from PDF's.
7 | Copyright 2017
8 | 1.3.0
9 | Damiaan Peeters, Tom van Enckevort
10 | ImageProcessor.Plugins.PDF
11 | https://github.com/dampee/ImageProcessor.Plugins.Pdf/blob/master/LICENSE
12 | https://github.com/dampee/ImageProcessor.Plugins.Pdf
13 | Pdfs are rendered as image, when a PDF has multiple pages, only the first is shown.
14 | ImageProcessor Plugin PDF Fileformat convert GhostScript thumbnail PDFs
15 | true
16 |
17 | https://github.com/dampee/ImageProcessor.Plugins.Pdf
18 | git
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/PdfFormat.cs:
--------------------------------------------------------------------------------
1 | using System.Drawing;
2 | using System.Drawing.Imaging;
3 | using System.IO;
4 | using System.Text;
5 | using ImageProcessor.Imaging.Formats;
6 |
7 | namespace ImageProcessor.Plugins.Pdf
8 | {
9 | ///
10 | /// The plugin enabling imageprocessor to read PDF as an image source
11 | ///
12 | public class PdfFormat : FormatBase
13 | {
14 | public PdfFormat()
15 | {
16 | DesiredDpiX = 96;
17 | DesiredDpiY = 96;
18 | }
19 |
20 | ///
21 | /// Desired X DPI for the result document
22 | ///
23 | public int DesiredDpiX { get; set; }
24 |
25 | ///
26 | /// Desired Y DPI for the result document
27 | ///
28 | public int DesiredDpiY { get; set; }
29 |
30 | ///
31 | /// Every PDF file starts with %PDF
32 | ///
33 | ///
34 | /// not according the specs, but in reality it does
35 | ///
36 | public override byte[][] FileHeaders => new[]
37 | {
38 | Encoding.UTF8.GetBytes("%PDF") // Must be 4 bytes or less in the current version of ImageProcessor
39 | };
40 |
41 | ///
42 | /// the default extention of a PDF file is "PDF"
43 | ///
44 | public new string DefaultExtension => "pdf";
45 |
46 | ///
47 | /// All the extensions which are supported
48 | ///
49 | public override string[] FileExtensions => new[] { "pdf" };
50 |
51 | ///
52 | /// the mimetype of the PNG output
53 | ///
54 | public override string MimeType => "image/png";
55 |
56 | ///
57 | /// The default output format
58 | ///
59 | public override ImageFormat ImageFormat => ImageFormat.Png;
60 |
61 | ///
62 | /// This method is used to load the pdf into the current stream
63 | ///
64 | ///
65 | ///
66 | public override Image Load(Stream stream)
67 | {
68 | var bytes = new byte[stream.Length];
69 | stream.Read(bytes, 0, bytes.Length);
70 | var rasterize = new PdfRasterizer(DesiredDpiX, DesiredDpiY);
71 | return rasterize.Rasterize(stream);
72 | }
73 | }
74 | }
--------------------------------------------------------------------------------
/PdfRasterizer.cs:
--------------------------------------------------------------------------------
1 | using System.Drawing;
2 | using System.IO;
3 | using Ghostscript.NET;
4 | using Ghostscript.NET.Rasterizer;
5 | using System;
6 | using System.Reflection;
7 |
8 | namespace ImageProcessor.Plugins.Pdf
9 | {
10 | internal class PdfRasterizer
11 | {
12 | private readonly int _desiredXDpi;
13 | private readonly int _desiredYDpi;
14 | private readonly GhostscriptVersionInfo _lastInstalledVersion;
15 | private readonly GhostscriptRasterizer _rasterizer;
16 |
17 | public PdfRasterizer(int xDpi = 96, int yDpi = 96)
18 | {
19 | _desiredXDpi = xDpi;
20 | _desiredYDpi = yDpi;
21 |
22 | var dllPath = Path.Combine(GetBinPath(), Environment.Is64BitProcess ? "gsdll64.dll" : "gsdll32.dll");
23 |
24 | if (File.Exists(dllPath))
25 | {
26 | // use DLL in bin folder
27 | _lastInstalledVersion = new GhostscriptVersionInfo(new System.Version(0, 0, 0), dllPath, string.Empty, GhostscriptLicense.GPL | GhostscriptLicense.AFPL);
28 | }
29 | else
30 | {
31 | // try to use installed DLL
32 | _lastInstalledVersion = GhostscriptVersionInfo.GetLastInstalledVersion(GhostscriptLicense.GPL | GhostscriptLicense.AFPL, GhostscriptLicense.GPL);
33 | }
34 |
35 | _rasterizer = new GhostscriptRasterizer();
36 | }
37 |
38 | public Image Rasterize(string pdfUri, int pageNumber = 1)
39 | {
40 | _rasterizer.Open(pdfUri, _lastInstalledVersion, true);
41 |
42 | var img = _rasterizer.GetPage(_desiredXDpi, _desiredYDpi, pageNumber);
43 |
44 | _rasterizer.Close();
45 | return img;
46 | }
47 |
48 | public Image Rasterize(Stream pdfStream, int pageNumber = 1)
49 | {
50 | _rasterizer.Open(pdfStream, _lastInstalledVersion, true);
51 |
52 | var img = _rasterizer.GetPage(_desiredXDpi, _desiredYDpi, pageNumber);
53 |
54 | _rasterizer.Close();
55 | return img;
56 | }
57 |
58 | private string GetBinPath()
59 | {
60 | string codeBase = Assembly.GetExecutingAssembly().CodeBase;
61 |
62 | UriBuilder uri = new UriBuilder(codeBase);
63 |
64 | string path = Uri.UnescapeDataString(uri.Path);
65 |
66 | return Path.GetDirectoryName(path);
67 | }
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 |
4 | # User-specific files
5 | *.suo
6 | *.user
7 | *.userosscache
8 | *.sln.docstates
9 |
10 | # User-specific files (MonoDevelop/Xamarin Studio)
11 | *.userprefs
12 |
13 | # Build results
14 | [Dd]ebug/
15 | [Dd]ebugPublic/
16 | [Rr]elease/
17 | [Rr]eleases/
18 | x64/
19 | x86/
20 | bld/
21 | [Bb]in/
22 | [Oo]bj/
23 | [Ll]og/
24 |
25 | # Visual Studio 2015 cache/options directory
26 | .vs/
27 | # Uncomment if you have tasks that create the project's static files in wwwroot
28 | #wwwroot/
29 |
30 | # MSTest test Results
31 | [Tt]est[Rr]esult*/
32 | [Bb]uild[Ll]og.*
33 |
34 | # NUNIT
35 | *.VisualState.xml
36 | TestResult.xml
37 |
38 | # Build Results of an ATL Project
39 | [Dd]ebugPS/
40 | [Rr]eleasePS/
41 | dlldata.c
42 |
43 | # DNX
44 | project.lock.json
45 | artifacts/
46 |
47 | *_i.c
48 | *_p.c
49 | *_i.h
50 | *.ilk
51 | *.meta
52 | *.obj
53 | *.pch
54 | *.pdb
55 | *.pgc
56 | *.pgd
57 | *.rsp
58 | *.sbr
59 | *.tlb
60 | *.tli
61 | *.tlh
62 | *.tmp
63 | *.tmp_proj
64 | *.log
65 | *.vspscc
66 | *.vssscc
67 | .builds
68 | *.pidb
69 | *.svclog
70 | *.scc
71 |
72 | # Chutzpah Test files
73 | _Chutzpah*
74 |
75 | # Visual C++ cache files
76 | ipch/
77 | *.aps
78 | *.ncb
79 | *.opendb
80 | *.opensdf
81 | *.sdf
82 | *.cachefile
83 | *.VC.db
84 | *.VC.VC.opendb
85 |
86 | # Visual Studio profiler
87 | *.psess
88 | *.vsp
89 | *.vspx
90 | *.sap
91 |
92 | # TFS 2012 Local Workspace
93 | $tf/
94 |
95 | # Guidance Automation Toolkit
96 | *.gpState
97 |
98 | # ReSharper is a .NET coding add-in
99 | _ReSharper*/
100 | *.[Rr]e[Ss]harper
101 | *.DotSettings.user
102 |
103 | # JustCode is a .NET coding add-in
104 | .JustCode
105 |
106 | # TeamCity is a build add-in
107 | _TeamCity*
108 |
109 | # DotCover is a Code Coverage Tool
110 | *.dotCover
111 |
112 | # NCrunch
113 | _NCrunch_*
114 | .*crunch*.local.xml
115 | nCrunchTemp_*
116 |
117 | # MightyMoose
118 | *.mm.*
119 | AutoTest.Net/
120 |
121 | # Web workbench (sass)
122 | .sass-cache/
123 |
124 | # Installshield output folder
125 | [Ee]xpress/
126 |
127 | # DocProject is a documentation generator add-in
128 | DocProject/buildhelp/
129 | DocProject/Help/*.HxT
130 | DocProject/Help/*.HxC
131 | DocProject/Help/*.hhc
132 | DocProject/Help/*.hhk
133 | DocProject/Help/*.hhp
134 | DocProject/Help/Html2
135 | DocProject/Help/html
136 |
137 | # Click-Once directory
138 | publish/
139 |
140 | # Publish Web Output
141 | *.[Pp]ublish.xml
142 | *.azurePubxml
143 | # TODO: Comment the next line if you want to checkin your web deploy settings
144 | # but database connection strings (with potential passwords) will be unencrypted
145 | *.pubxml
146 | *.publishproj
147 |
148 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
149 | # checkin your Azure Web App publish settings, but sensitive information contained
150 | # in these scripts will be unencrypted
151 | PublishScripts/
152 |
153 | # NuGet Packages
154 | *.nupkg
155 | # The packages folder can be ignored because of Package Restore
156 | **/packages/*
157 | # except build/, which is used as an MSBuild target.
158 | !**/packages/build/
159 | # Uncomment if necessary however generally it will be regenerated when needed
160 | #!**/packages/repositories.config
161 | # NuGet v3's project.json files produces more ignoreable files
162 | *.nuget.props
163 | *.nuget.targets
164 |
165 | # Microsoft Azure Build Output
166 | csx/
167 | *.build.csdef
168 |
169 | # Microsoft Azure Emulator
170 | ecf/
171 | rcf/
172 |
173 | # Windows Store app package directories and files
174 | AppPackages/
175 | BundleArtifacts/
176 | Package.StoreAssociation.xml
177 | _pkginfo.txt
178 |
179 | # Visual Studio cache files
180 | # files ending in .cache can be ignored
181 | *.[Cc]ache
182 | # but keep track of directories ending in .cache
183 | !*.[Cc]ache/
184 |
185 | # Others
186 | ClientBin/
187 | ~$*
188 | *~
189 | *.dbmdl
190 | *.dbproj.schemaview
191 | *.pfx
192 | *.publishsettings
193 | node_modules/
194 | orleans.codegen.cs
195 |
196 | # Since there are multiple workflows, uncomment next line to ignore bower_components
197 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
198 | #bower_components/
199 |
200 | # RIA/Silverlight projects
201 | Generated_Code/
202 |
203 | # Backup & report files from converting an old project file
204 | # to a newer Visual Studio version. Backup files are not needed,
205 | # because we have git ;-)
206 | _UpgradeReport_Files/
207 | Backup*/
208 | UpgradeLog*.XML
209 | UpgradeLog*.htm
210 |
211 | # SQL Server files
212 | *.mdf
213 | *.ldf
214 |
215 | # Business Intelligence projects
216 | *.rdl.data
217 | *.bim.layout
218 | *.bim_*.settings
219 |
220 | # Microsoft Fakes
221 | FakesAssemblies/
222 |
223 | # GhostDoc plugin setting file
224 | *.GhostDoc.xml
225 |
226 | # Node.js Tools for Visual Studio
227 | .ntvs_analysis.dat
228 |
229 | # Visual Studio 6 build log
230 | *.plg
231 |
232 | # Visual Studio 6 workspace options file
233 | *.opt
234 |
235 | # Visual Studio LightSwitch build output
236 | **/*.HTMLClient/GeneratedArtifacts
237 | **/*.DesktopClient/GeneratedArtifacts
238 | **/*.DesktopClient/ModelManifest.xml
239 | **/*.Server/GeneratedArtifacts
240 | **/*.Server/ModelManifest.xml
241 | _Pvt_Extensions
242 |
243 | # Paket dependency manager
244 | .paket/paket.exe
245 | paket-files/
246 |
247 | # FAKE - F# Make
248 | .fake/
249 |
250 | # JetBrains Rider
251 | .idea/
252 | *.sln.iml
253 |
--------------------------------------------------------------------------------