Skip to content

Commit

Permalink
Fix password issue #43; add password unit tests; introduce PDF specif…
Browse files Browse the repository at this point in the history
…ic exceptions (#44)
  • Loading branch information
sungaila authored Sep 25, 2023
1 parent a1f57f5 commit 17d803f
Show file tree
Hide file tree
Showing 47 changed files with 3,207 additions and 2,944 deletions.
31 changes: 18 additions & 13 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@ jobs:
- name: Setup .NET 7
uses: actions/setup-dotnet@main
with:
dotnet-version: 7.0.306 # TEMP FIX: SDK 7.0.400 broke the Android builds
- name: Setup .NET workloads
run: dotnet workload restore src/PDFtoImage.sln
dotnet-version: 7.x
- name: Setup .NET workload android
run: dotnet workload install android
- name: Setup .NET workload wasm-tools
run: dotnet workload install wasm-tools
- name: Setup JDK 17
uses: actions/setup-java@main
with:
Expand All @@ -92,13 +94,6 @@ jobs:
!**/*.nupkg
!**/*.snupkg
if-no-files-found: error
- name: Publish tests
uses: actions/upload-artifact@main
with:
name: Test assemblies
path: src/Tests/bin/${{ github.event_name != 'workflow_dispatch' && 'Debug' || inputs.build_configuration }}
if-no-files-found: error
retention-days: 1
- name: Publish NuGet packages
uses: actions/upload-artifact@main
with:
Expand All @@ -107,6 +102,14 @@ jobs:
src/PDFtoImage/bin/${{ github.event_name != 'workflow_dispatch' && 'Debug' || inputs.build_configuration }}/*.nupkg
src/PDFtoImage/bin/${{ github.event_name != 'workflow_dispatch' && 'Debug' || inputs.build_configuration }}/*.snupkg
if-no-files-found: error
- name: Publish tests
uses: actions/upload-artifact@main
if: success() && (github.event_name != 'workflow_dispatch' && true || inputs.run_tests) == true
with:
name: Test assemblies
path: src/Tests/bin/${{ github.event_name != 'workflow_dispatch' && 'Debug' || inputs.build_configuration }}
if-no-files-found: error
retention-days: 1
test:
name: Test (${{ matrix.os }})
needs: build
Expand Down Expand Up @@ -206,11 +209,13 @@ jobs:
- name: Setup .NET 7
uses: actions/setup-dotnet@main
with:
dotnet-version: 7.0.306 # TEMP FIX: SDK 7.0.400 broke the Android builds
dotnet-version: 7.x
- name: Setup dotnet-coverage
run: dotnet tool install --global dotnet-coverage
- name: Setup .NET workloads
run: dotnet workload restore src/PDFtoImage.sln
- name: Setup .NET workload android
run: dotnet workload install android
- name: Setup .NET workload wasm-tools
run: dotnet workload install wasm-tools
- name: Setup JDK 17
uses: actions/setup-java@main
with:
Expand Down
4 changes: 1 addition & 3 deletions .github/workflows/githubpages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ jobs:
uses: actions/setup-dotnet@main
with:
dotnet-version: 7.x
- name: Setup .NET workload android
run: dotnet workload install android
- name: Setup .NET workload wasm-tools
run: dotnet workload install wasm-tools
- name: Update relative paths
Expand Down Expand Up @@ -96,4 +94,4 @@ jobs:
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@main
uses: actions/deploy-pages@main
2 changes: 1 addition & 1 deletion src/Console/Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<AssemblyName>PDFtoImage.Console</AssemblyName>
<RootNamespace>PDFtoImage.Console</RootNamespace>
<StartupObject>PDFtoImage.Console.Program</StartupObject>
<Version>2.4.0</Version>
<Version>2.4.1</Version>
<Configurations>Debug;Release;ReleaseSigned</Configurations>
</PropertyGroup>

Expand Down
2,286 changes: 1,143 additions & 1,143 deletions src/PDFtoImage/Conversion.cs

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions src/PDFtoImage/Exceptions/PdfCannotOpenFileException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using static PDFtoImage.PdfiumViewer.NativeMethods;

namespace PDFtoImage.Exceptions
{
/// <summary>
/// Thrown if the PDF file was not found or could not be opened.
/// </summary>
public class PdfCannotOpenFileException : PdfException
{
internal override FPDF_ERR Error => FPDF_ERR.FILE;

/// <inheritdoc/>
public PdfCannotOpenFileException() : base("File not found or could not be opened.") { }
}
}
28 changes: 28 additions & 0 deletions src/PDFtoImage/Exceptions/PdfException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using static PDFtoImage.PdfiumViewer.NativeMethods;

namespace PDFtoImage.Exceptions
{
/// <summary>
/// Base class for all PDF related exceptions.
/// </summary>
public abstract class PdfException : Exception
{
internal abstract FPDF_ERR Error { get; }

/// <inheritdoc/>
protected PdfException(string message) : base(message) { }

internal static PdfException? CreateException(FPDF_ERR error) => error switch
{
FPDF_ERR.SUCCESS => null,
FPDF_ERR.UNKNOWN => new PdfUnknownException(),
FPDF_ERR.FILE => new PdfCannotOpenFileException(),
FPDF_ERR.FORMAT => new PdfInvalidFormatException(),
FPDF_ERR.PASSWORD => new PdfPasswordProtectedException(),
FPDF_ERR.SECURITY => new PdfUnsupportedSecuritySchemeException(),
FPDF_ERR.PAGE => new PdfPageNotFoundException(),
_ => new PdfUnknownException()
};
}
}
15 changes: 15 additions & 0 deletions src/PDFtoImage/Exceptions/PdfInvalidFormatException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using static PDFtoImage.PdfiumViewer.NativeMethods;

namespace PDFtoImage.Exceptions
{
/// <summary>
/// Thrown if the PDF format is invalid or corrupted.
/// </summary>
public class PdfInvalidFormatException : PdfException
{
internal override FPDF_ERR Error => FPDF_ERR.FORMAT;

/// <inheritdoc/>
public PdfInvalidFormatException() : base("File not in PDF format or corrupted.") { }
}
}
15 changes: 15 additions & 0 deletions src/PDFtoImage/Exceptions/PdfPageNotFoundException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using static PDFtoImage.PdfiumViewer.NativeMethods;

namespace PDFtoImage.Exceptions
{
/// <summary>
/// Thrown if the PDF does not contain the given page number.
/// </summary>
public class PdfPageNotFoundException : PdfException
{
internal override FPDF_ERR Error => FPDF_ERR.PAGE;

/// <inheritdoc/>
public PdfPageNotFoundException() : base("Page not found or content error.") { }
}
}
15 changes: 15 additions & 0 deletions src/PDFtoImage/Exceptions/PdfPasswordProtectedException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using static PDFtoImage.PdfiumViewer.NativeMethods;

namespace PDFtoImage.Exceptions
{
/// <summary>
/// Thrown if the PDF requires a password and the given password was not given or incorrect.
/// </summary>
public class PdfPasswordProtectedException : PdfException
{
internal override FPDF_ERR Error => FPDF_ERR.PASSWORD;

/// <inheritdoc/>
public PdfPasswordProtectedException() : base("Password required or incorrect password.") { }
}
}
15 changes: 15 additions & 0 deletions src/PDFtoImage/Exceptions/PdfUnknownException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using static PDFtoImage.PdfiumViewer.NativeMethods;

namespace PDFtoImage.Exceptions
{
/// <summary>
/// Thrown on unknown PDF errors.
/// </summary>
public class PdfUnknownException : PdfException
{
internal override FPDF_ERR Error => FPDF_ERR.UNKNOWN;

/// <inheritdoc/>
public PdfUnknownException() : base("Unknown error.") { }
}
}
15 changes: 15 additions & 0 deletions src/PDFtoImage/Exceptions/PdfUnsupportedSecuritySchemeException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using static PDFtoImage.PdfiumViewer.NativeMethods;

namespace PDFtoImage.Exceptions
{
/// <summary>
/// Thrown if the PDF file uses an unsupported security scheme.
/// </summary>
public class PdfUnsupportedSecuritySchemeException : PdfException
{
internal override FPDF_ERR Error => FPDF_ERR.SECURITY;

/// <inheritdoc/>
public PdfUnsupportedSecuritySchemeException() : base("Unsupported security scheme.") { }
}
}
29 changes: 15 additions & 14 deletions src/PDFtoImage/PDFtoImage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<!-- NuGet -->
<PropertyGroup>
<VersionPrefix>2.4.0</VersionPrefix>
<VersionPrefix>2.4.1</VersionPrefix>
<VersionSuffix></VersionSuffix>
<Authors>David Sungaila</Authors>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
Expand All @@ -21,7 +21,8 @@
<PackageProjectUrl>https://github.com/sungaila/PDFtoImage</PackageProjectUrl>
<PackageIconUrl>https://raw.githubusercontent.com/sungaila/PDFtoImage/master/etc/Icon_128.png</PackageIconUrl>
<Description>A .NET library to render PDF files into images.</Description>
<PackageReleaseNotes>- Added support for WebAssembly (e.g. ASP.NET Core Blazor WebAssembly).</PackageReleaseNotes>
<PackageReleaseNotes>- Fixed an issue where password protected PDF files could not be opened.
- Added PDF specific Exception classes (e.g. PdfPasswordProtectedException).</PackageReleaseNotes>
<PackageTags>PDF Bitmap Image Convert Conversion C# PDFium SkiaSharp Skia PNG JPG JPEG WEBP Xamarin Android MonoAndroid MAUI WASM WebAssembly</PackageTags>
<RepositoryUrl>https://github.com/sungaila/PDFtoImage.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
Expand Down Expand Up @@ -66,7 +67,7 @@

<!-- SourceLink build steps and NuGet packages -->
<ItemGroup>
<PackageReference Include="SkiaSharp" Version="2.88.5" PrivateAssets="analyzers" />
<PackageReference Include="SkiaSharp" Version="2.88.6" PrivateAssets="analyzers" />
<PackageReference Include="Microsoft.CodeAnalysis.PublicApiAnalyzers" Version="3.3.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand All @@ -83,25 +84,25 @@
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)'=='net462' or '$(TargetFramework)'=='net481'">
<PackageReference Include="bblanchon.PDFium.Win32" Version="118.0.5975" PrivateAssets="analyzers" />
<PackageReference Include="SkiaSharp.NativeAssets.Win32" Version="2.88.5" PrivateAssets="analyzers" />
<PackageReference Include="bblanchon.PDFium.Win32" Version="119.0.6029" PrivateAssets="analyzers" />
<PackageReference Include="SkiaSharp.NativeAssets.Win32" Version="2.88.6" PrivateAssets="analyzers" />
</ItemGroup>

<!-- .NET packages -->
<ItemGroup Condition="'$(TargetFramework)'=='net6.0' or '$(TargetFramework)'=='net7.0'">
<PackageReference Include="bblanchon.PDFium.Linux" Version="118.0.5975" PrivateAssets="analyzers" />
<PackageReference Include="bblanchon.PDFium.macOS" Version="118.0.5975" PrivateAssets="analyzers" />
<PackageReference Include="bblanchon.PDFium.Win32" Version="118.0.5975" PrivateAssets="analyzers" />
<PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="2.88.5" PrivateAssets="analyzers" />
<PackageReference Include="SkiaSharp.NativeAssets.macOS" Version="2.88.5" PrivateAssets="analyzers" />
<PackageReference Include="SkiaSharp.NativeAssets.Win32" Version="2.88.5" PrivateAssets="analyzers" />
<PackageReference Include="SkiaSharp.NativeAssets.WebAssembly" Version="2.88.5" PrivateAssets="analyzers" />
<PackageReference Include="bblanchon.PDFium.Linux" Version="119.0.6029" PrivateAssets="analyzers" />
<PackageReference Include="bblanchon.PDFium.macOS" Version="119.0.6029" PrivateAssets="analyzers" />
<PackageReference Include="bblanchon.PDFium.Win32" Version="119.0.6029" PrivateAssets="analyzers" />
<PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="2.88.6" PrivateAssets="analyzers" />
<PackageReference Include="SkiaSharp.NativeAssets.macOS" Version="2.88.6" PrivateAssets="analyzers" />
<PackageReference Include="SkiaSharp.NativeAssets.Win32" Version="2.88.6" PrivateAssets="analyzers" />
<PackageReference Include="SkiaSharp.NativeAssets.WebAssembly" Version="2.88.6" PrivateAssets="analyzers" />
</ItemGroup>

<!-- Android packages -->
<ItemGroup Condition="'$(TargetFramework)'=='net7.0-android' or '$(TargetFramework)'=='monoandroid10.0'">
<PackageReference Include="bblanchon.PDFium.Android" Version="118.0.5975" PrivateAssets="analyzers" />
<PackageReference Include="SkiaSharp.NativeAssets.Android" Version="2.88.5" PrivateAssets="analyzers" />
<PackageReference Include="bblanchon.PDFium.Android" Version="119.0.6029" PrivateAssets="analyzers" />
<PackageReference Include="SkiaSharp.NativeAssets.Android" Version="2.88.6" PrivateAssets="analyzers" />
</ItemGroup>

<Import Project="PDFtoImage.PropertiesSigning.targets" />
Expand Down
44 changes: 22 additions & 22 deletions src/PDFtoImage/PdfRotation.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
namespace PDFtoImage
{
/// <summary>
/// Specifies the rotation of pages shown in the PDF renderer.
/// </summary>
public enum PdfRotation
{
/// <summary>
/// No rotation.
/// </summary>
Rotate0 = 0,
/// <summary>
/// Specifies the rotation of pages shown in the PDF renderer.
/// </summary>
public enum PdfRotation
{
/// <summary>
/// No rotation.
/// </summary>
Rotate0 = 0,

/// <summary>
/// Rotated 90 degrees clockwise.
/// </summary>
Rotate90 = 1,
/// <summary>
/// Rotated 90 degrees clockwise.
/// </summary>
Rotate90 = 1,

/// <summary>
/// Rotated 180 degrees.
/// </summary>
Rotate180 = 2,
/// <summary>
/// Rotated 180 degrees.
/// </summary>
Rotate180 = 2,

/// <summary>
/// Rotated 90 degrees counter-clockwise.
/// </summary>
Rotate270 = 3
}
/// <summary>
/// Rotated 90 degrees counter-clockwise.
/// </summary>
Rotate270 = 3
}
}
43 changes: 35 additions & 8 deletions src/PDFtoImage/PdfiumViewer/NativeMethods.Pdfium.cs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ private static class Imports
[DllImport(_dllName, CallingConvention = CallingConvention.Cdecl)]
public static extern void FPDF_DestroyLibrary();

[DllImport(_dllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
[DllImport(_dllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr FPDF_LoadCustomDocument([MarshalAs(UnmanagedType.LPStruct)] FPDF_FILEACCESS access, string? password);

[DllImport(_dllName, CallingConvention = CallingConvention.Cdecl)]
Expand Down Expand Up @@ -563,13 +563,40 @@ public enum FPDF

public enum FPDF_ERR : uint
{
SUCCESS = 0, // No error.
UNKNOWN = 1, // Unknown error.
FILE = 2, // File not found or could not be opened.
FORMAT = 3, // File not in PDF format or corrupted.
PASSWORD = 4, // Password required or incorrect password.
SECURITY = 5, // Unsupported security scheme.
PAGE = 6 // Page not found or content error.
/// <summary>
/// No error.
/// </summary>
SUCCESS = 0,

/// <summary>
/// Unknown error.
/// </summary>
UNKNOWN = 1,

/// <summary>
/// File not found or could not be opened.
/// </summary>
FILE = 2,

/// <summary>
/// File not in PDF format or corrupted.
/// </summary>
FORMAT = 3,

/// <summary>
/// Password required or incorrect password.
/// </summary>
PASSWORD = 4,

/// <summary>
/// Unsupported security scheme.
/// </summary>
SECURITY = 5,

/// <summary>
/// Page not found or content error.
/// </summary>
PAGE = 6
}

#region Save/Edit Structs and Flags
Expand Down
Loading

0 comments on commit 17d803f

Please sign in to comment.