Skip to content

Commit

Permalink
Add support for linux-x64, fixes .NET Framework assembly issues and u…
Browse files Browse the repository at this point in the history
…pdates pdfium binaries
  • Loading branch information
sungaila committed Feb 28, 2021
1 parent a6c10e1 commit db514cc
Show file tree
Hide file tree
Showing 13 changed files with 129 additions and 62 deletions.
5 changes: 2 additions & 3 deletions Console/Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
<AssemblyName>PDFtoZPL.Console</AssemblyName>
<RootNamespace>PDFtoZPL.Console</RootNamespace>
<StartupObject>PDFtoZPL.Console.Program</StartupObject>
<Version>1.1.0</Version>
<Version>1.2.0</Version>
</PropertyGroup>

<!-- C# compiler -->
<PropertyGroup>
<LangVersion>8.0</LangVersion>
<LangVersion>9.0</LangVersion>
<Nullable>enable</Nullable>
<Features>strict</Features>
<WarningsAsErrors>nullable</WarningsAsErrors>
Expand All @@ -23,5 +23,4 @@
<ItemGroup>
<ProjectReference Include="..\PDFtoZPL\PDFtoZPL.csproj" />
</ItemGroup>

</Project>
4 changes: 3 additions & 1 deletion Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ static int Main(string[] args)

string zpl = (Path.GetExtension(inputPath).ToLower()) switch
{
".pdf" => Conversion.ConvertPdfPage(inputStream),
".pdf" => OperatingSystem.IsWindows() || OperatingSystem.IsLinux()
? Conversion.ConvertPdfPage(inputStream)
: throw new NotSupportedException("Only win-x86, win-x64 and linux-x64 are supported for PDF file conversion."),
".bmp" => Conversion.ConvertBitmap(inputStream),
_ => throw new InvalidOperationException("The given input file path must have pdf or bmp as file extension."),
};
Expand Down
13 changes: 13 additions & 0 deletions PDFtoZPL/Conversion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Text;

namespace PDFtoZPL
Expand All @@ -24,6 +25,10 @@ public static class Conversion
/// <param name="width">The width of the desired <paramref name="page"/>. Use <see langword="null"/> if the original width should be used.</param>
/// <param name="height">The height of the desired <paramref name="page"/>. Use <see langword="null"/> if the original height should be used.</param>
/// <returns>The converted PDF page as ZPL code.</returns>
#if !NETSTANDARD
[SupportedOSPlatform("Windows")]
[SupportedOSPlatform("Linux")]
#endif
public static string ConvertPdfPage(string pdfAsBase64String, string? password = null, int page = 0, int dpi = 203, int? width = null, int? height = null)
{
if (pdfAsBase64String == null)
Expand All @@ -42,6 +47,10 @@ public static string ConvertPdfPage(string pdfAsBase64String, string? password =
/// <param name="width">The width of the desired <paramref name="page"/>. Use <see langword="null"/> if the original width should be used.</param>
/// <param name="height">The height of the desired <paramref name="page"/>. Use <see langword="null"/> if the original height should be used.</param>
/// <returns>The converted PDF page as ZPL code.</returns>
#if !NETSTANDARD
[SupportedOSPlatform("Windows")]
[SupportedOSPlatform("Linux")]
#endif
public static string ConvertPdfPage(byte[] pdfAsByteArray, string? password = null, int page = 0, int dpi = 203, int? width = null, int? height = null)
{
if (pdfAsByteArray == null)
Expand All @@ -63,6 +72,10 @@ public static string ConvertPdfPage(byte[] pdfAsByteArray, string? password = nu
/// <param name="width">The width of the desired <paramref name="page"/>. Use <see langword="null"/> if the original width should be used.</param>
/// <param name="height">The height of the desired <paramref name="page"/>. Use <see langword="null"/> if the original height should be used.</param>
/// <returns>The converted PDF page as ZPL code.</returns>
#if !NETSTANDARD
[SupportedOSPlatform("Windows")]
[SupportedOSPlatform("Linux")]
#endif
public static string ConvertPdfPage(Stream pdfStream, string? password = null, int page = 0, int dpi = 203, int? width = null, int? height = null)
{
if (pdfStream == null)
Expand Down
48 changes: 30 additions & 18 deletions PDFtoZPL/PDFtoZPL.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<!-- Assembly -->
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net50-windows</TargetFrameworks>
<RuntimeIdentifiers>win-x86;win-x64</RuntimeIdentifiers>
<TargetFrameworks>netstandard2.0;net50</TargetFrameworks>
<RuntimeIdentifiers>win-x86;win-x64;linux-x64</RuntimeIdentifiers>
<AssemblyName>PDFtoZPL</AssemblyName>
<RootNamespace>PDFtoZPL</RootNamespace>
<EmbedAllSources>true</EmbedAllSources>
Expand All @@ -12,15 +12,16 @@

<!-- NuGet -->
<PropertyGroup>
<VersionPrefix>1.1.0</VersionPrefix>
<VersionPrefix>1.2.0</VersionPrefix>
<VersionSuffix>preview</VersionSuffix>
<Authors>David Sungaila</Authors>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageIcon>Icon_128.png</PackageIcon>
<PackageProjectUrl>https://github.com/sungaila/PDFtoZPL</PackageProjectUrl>
<PackageIconUrl>https://raw.githubusercontent.com/sungaila/PDFtoZPL/master/Icon_128.png</PackageIconUrl>
<Description>A .NET Standard library to convert PDF files and bitmap images into Zebra Programming Language code.</Description>
<PackageReleaseNotes>Added .NET 5.0 as a target framework.</PackageReleaseNotes>
<PackageReleaseNotes>Adds support for Linux x86-64 and fixes .NET Framework targets.</PackageReleaseNotes>
<PackageTags>PDF ZPL Zebra Bitmap Convert Conversion C#</PackageTags>
<RepositoryUrl>https://github.com/sungaila/PDFtoZPL.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
Expand All @@ -30,7 +31,7 @@

<!-- C# compiler -->
<PropertyGroup>
<LangVersion>8.0</LangVersion>
<LangVersion>9.0</LangVersion>
<Nullable>enable</Nullable>
<Features>strict</Features>
<WarningsAsErrors>nullable</WarningsAsErrors>
Expand All @@ -49,38 +50,49 @@
<Optimize>true</Optimize>
</PropertyGroup>

<!-- NuGet Icon -->
<ItemGroup>
<None Include="..\Icon_128.png">
<Pack>True</Pack>
<Pack>true</Pack>
<PackagePath></PackagePath>
</None>
</ItemGroup>

<ItemGroup>
<None Include="PDFtoZPL.targets">
<Pack>True</Pack>
<Pack>true</Pack>
<PackagePath>build</PackagePath>
</None>
<Content Include="runtimes\win-x64\native\pdfium.dll">
<None Include="runtimes\win-x64\native\pdfium.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Pack>true</Pack>
<PackagePath>runtimes\win-x64\native\pdfium.dll</PackagePath>
</Content>
<Content Include="runtimes\win-x86\native\pdfium.dll">
<Visible>false</Visible>
</None>
<None Include="runtimes\win-x86\native\pdfium.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Pack>true</Pack>
<PackagePath>runtimes\win-x86\native\pdfium.dll</PackagePath>
</Content>
<Visible>false</Visible>
</None>
<None Include="runtimes\linux-x64\native\libpdfium.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Pack>true</Pack>
<PackagePath>runtimes\linux-x64\native\libpdfium.so</PackagePath>
<Visible>false</Visible>
</None>
</ItemGroup>

<!-- NuGet Icon -->
<ItemGroup>
<None Include="..\Icon_128.png">
<Pack>true</Pack>
<PackagePath></PackagePath>
</None>
</ItemGroup>

<!-- SourceLink build steps and NuGet packages -->
<ItemGroup>
<PackageReference Include="System.Drawing.Common" Version="5.0.0" />
<PackageReference Include="System.Drawing.Common" Version="5.0.1" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>
</Project>
18 changes: 10 additions & 8 deletions PDFtoZPL/PDFtoZPL.targets
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- this forces .NET Framework targets to copy the native pdfium assemblies -->
<!-- .NET (Core) supports the runtimes folder out of the box -->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Content Include="$(MSBuildThisFileDirectory)..\runtimes\win-x86\native\pdfium.dll">
<ItemGroup Condition="'$(TargetFramework)'=='net461' or '$(TargetFramework)'=='net462' or '$(TargetFramework)'=='net47' or '$(TargetFramework)'=='net471' or '$(TargetFramework)'=='net472' or '$(TargetFramework)'=='net48' or '$(TargetFrameworkVersion)'=='v4.6.1' or '$(TargetFrameworkVersion)'=='v4.6.2' or '$(TargetFrameworkVersion)'=='v4.7' or '$(TargetFrameworkVersion)'=='v4.7.1' or '$(TargetFrameworkVersion)'=='v4.7.2' or '$(TargetFrameworkVersion)'=='v4.8'">
<None Include="$(MSBuildThisFileDirectory)..\runtimes\win-x86\native\pdfium.dll" >
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>pdfium.dll</Link>
<Link>runtimes\win-x86\native\pdfium.dll</Link>
<Visible>false</Visible>
</Content>
<Content Include="$(MSBuildThisFileDirectory)..\runtimes\win-x64\native\pdfium.dll">
</None>
<None Include="$(MSBuildThisFileDirectory)..\runtimes\win-x64\native\pdfium.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>pdfium.dll</Link>
<Link>runtimes\win-x64\native\pdfium.dll</Link>
<Visible>false</Visible>
</Content>
</None>
</ItemGroup>
</Project>
</Project>
18 changes: 9 additions & 9 deletions PDFtoZPL/PdfiumViewer/NativeMethods.Pdfium.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ internal partial class NativeMethods
// threads, even when there are multiple AppDomain's in play.
private static readonly string LockString = String.Intern("e362349b-001d-4cb2-bf55-a71606a3e36f");

public static void FPDF_AddRef()
public static void FPDF_InitLibrary()
{
lock (LockString)
{
Imports.FPDF_AddRef();
Imports.FPDF_InitLibrary();
}
}

public static void FPDF_Release()
public static void FPDF_DestroyLibrary()
{
lock (LockString)
{
Imports.FPDF_Release();
Imports.FPDF_DestroyLibrary();
}
}

Expand Down Expand Up @@ -229,11 +229,11 @@ public static IntPtr FPDFBitmap_Destroy(IntPtr bitmapHandle)
}
}

public static uint FPDFDest_GetPageIndex(IntPtr document, IntPtr dest)
public static uint FPDFDest_GetDestPageIndex(IntPtr document, IntPtr dest)
{
lock (LockString)
{
return Imports.FPDFDest_GetPageIndex(document, dest);
return Imports.FPDFDest_GetDestPageIndex(document, dest);
}
}

Expand Down Expand Up @@ -333,10 +333,10 @@ private static int FPDF_GetBlock(IntPtr param, uint position, IntPtr buffer, uin
private static class Imports
{
[DllImport("pdfium.dll")]
public static extern void FPDF_AddRef();
public static extern void FPDF_InitLibrary();

[DllImport("pdfium.dll")]
public static extern void FPDF_Release();
public static extern void FPDF_DestroyLibrary();

[DllImport("pdfium.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr FPDF_LoadCustomDocument([MarshalAs(UnmanagedType.LPStruct)] FPDF_FILEACCESS access, string? password);
Expand Down Expand Up @@ -417,7 +417,7 @@ private static class Imports
public static extern IntPtr FPDFBitmap_Destroy(IntPtr bitmapHandle);

[DllImport("pdfium.dll")]
public static extern uint FPDFDest_GetPageIndex(IntPtr document, IntPtr dest);
public static extern uint FPDFDest_GetDestPageIndex(IntPtr document, IntPtr dest);

[DllImport("pdfium.dll")]
public static extern IntPtr FPDFBookmark_GetFirstChild(IntPtr document, IntPtr bookmark);
Expand Down
39 changes: 36 additions & 3 deletions PDFtoZPL/PdfiumViewer/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,42 @@ private static bool TryLoadNativeLibrary(string path)
return false;

path = Path.Combine(path, "runtimes");
path = Path.Combine(path, IntPtr.Size == 4 ? "win-x86" : "win-x64");
path = Path.Combine(path, "native");
path = Path.Combine(path, "Pdfium.dll");

#if !NETSTANDARD
if (OperatingSystem.IsWindows())
{
path = Path.Combine(path, Environment.Is64BitProcess ? "win-x64" : "win-x86");
path = Path.Combine(path, "native");
path = Path.Combine(path, "pdfium.dll");
}
else if (OperatingSystem.IsLinux())
{
path = Path.Combine(path, Environment.Is64BitProcess ? "linux-x64" : throw new NotSupportedException("Only x86-64 is supported on Linux."));
path = Path.Combine(path, "native");
path = Path.Combine(path, "libpdfium.so");
}
else
{
throw new NotSupportedException("Only win-x86, win-x64 and linux-x64 are supported.");
}
#else
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
path = Path.Combine(path, Environment.Is64BitProcess ? "win-x64" : "win-x86");
path = Path.Combine(path, "native");
path = Path.Combine(path, "pdfium.dll");
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
path = Path.Combine(path, Environment.Is64BitProcess ? "linux-x64" : throw new NotSupportedException("Only x86-64 is supported on Linux."));
path = Path.Combine(path, "native");
path = Path.Combine(path, "libpdfium.so");
}
else
{
throw new NotSupportedException("Only win-x86, win-x64 and linux-x64 are supported.");
}
#endif

return File.Exists(path) && LoadLibrary(path) != IntPtr.Zero;
}
Expand Down
2 changes: 1 addition & 1 deletion PDFtoZPL/PdfiumViewer/PdfFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ private uint GetBookmarkPageIndex(IntPtr bookmark)
{
IntPtr dest = NativeMethods.FPDF_BookmarkGetDest(_document, bookmark);
if (dest != IntPtr.Zero)
return NativeMethods.FPDFDest_GetPageIndex(_document, dest);
return NativeMethods.FPDFDest_GetDestPageIndex(_document, dest);

return 0;
}
Expand Down
29 changes: 13 additions & 16 deletions PDFtoZPL/PdfiumViewer/PdfLibrary.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System;
using System;

namespace PDFtoZPL.PdfiumViewer
{
internal class PdfLibrary : IDisposable
{
private static readonly object _syncRoot = new object();
private static PdfLibrary? _library;
private bool disposedValue;

public static void EnsureLoaded()
{
Expand All @@ -16,33 +17,29 @@ public static void EnsureLoaded()
}
}

private bool _disposed;

private PdfLibrary()
{
NativeMethods.FPDF_AddRef();
NativeMethods.FPDF_InitLibrary();
}

~PdfLibrary()
{
Dispose(false);
Dispose(disposing: false);
}

public void Dispose()
protected virtual void Dispose(bool disposing)
{
Dispose(true);

GC.SuppressFinalize(this);
if (!disposedValue)
{
NativeMethods.FPDF_DestroyLibrary();
disposedValue = true;
}
}

private void Dispose(bool disposing)
public void Dispose()
{
if (!_disposed)
{
NativeMethods.FPDF_Release();

_disposed = true;
}
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}
Binary file added PDFtoZPL/runtimes/linux-x64/native/libpdfium.so
Binary file not shown.
Binary file modified PDFtoZPL/runtimes/win-x64/native/pdfium.dll
Binary file not shown.
Binary file modified PDFtoZPL/runtimes/win-x86/native/pdfium.dll
Binary file not shown.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,17 @@
[![NuGet downloads](https://img.shields.io/nuget/dt/PDFtoZPL.svg?style=flat-square)](https://www.nuget.org/packages/PDFtoZPL/)
[![GitHub license](https://img.shields.io/github/license/sungaila/PDFtoZPL?style=flat-square)](https://github.com/sungaila/PDFtoZPL/blob/master/LICENSE)

A .NET Standard library to convert [PDF files](https://en.wikipedia.org/wiki/PDF) into [Zebra Programming Language commands](https://en.wikipedia.org/wiki/Zebra_(programming_language)).
A .NET Standard library to convert [PDF files](https://en.wikipedia.org/wiki/PDF) (or bitmaps) into [Zebra Programming Language commands](https://en.wikipedia.org/wiki/Zebra_(programming_language)).

First your PDF is rasterized into a monochrome bitmap image. Then the ^GF (Graphic Field) command is applied to the bitmap. The bitmap is compressed to shrink the ZPL code in size.
This .NET library is built on top of
* [PDFium](https://pdfium.googlesource.com/pdfium/) (native PDF renderer)
* [PdfiumViewer](https://github.com/pvginkel/PdfiumViewer) (wrapper for pdfium)

The PDF rasterizer used in this project is a stripped down version of [PdfiumViewer](https://github.com/pvginkel/PdfiumViewer) which is built on top of [PDFium](https://pdfium.googlesource.com/pdfium/). I recommend the [Labelary Online ZPL Viewer](http://labelary.com/viewer.html) to check the created ZPL code. Only Windows x86 and x86-64 are supported as platforms.
You can use [Labelary Online ZPL Viewer](http://labelary.com/viewer.html) to render the resulting ZPL code.

### How does it work?
0. Use PDFium to render a bitmap (for PDF files)
1. Make the bitmap monochrome
2. Convert the bitmap into a ^GF (Graphic Field) command
3. Compress the command hexdecimal data to shrink the ZPL code in size
4. Return the generated ZPL code

0 comments on commit db514cc

Please sign in to comment.