I'm trying to use Ghostscript.NET to convert a PDF into an image on macOS. Here's my code:
using System;
using System.IO;
using Ghostscript.NET.Rasterizer;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Formats.Png;
public class PdfToImageConverter
{
public static void ConvertPdfToThumbnail(string pdfPath, string outputImagePath, int pageNumber = 1, int dpi = 100)
{
using (var rasterizer = new GhostscriptRasterizer())
{
rasterizer.Open(pdfPath);
using (var img = rasterizer.GetPage(dpi, pageNumber))
{
using (var memoryStream = new MemoryStream())
{
img.Save(outputImagePath, System.Drawing.Imaging.ImageFormat.Png);
memoryStream.Seek(0, SeekOrigin.Begin);
using (var image = SixLabors.ImageSharp.Image.Load<Rgba32>(memoryStream))
{
image.Save(outputImagePath, new PngEncoder());
}
}
}
}
}
public static void Main()
{
string pdfPath = @"/Users/Downloads/testfile.pdf";
string outputPath = @"/Users/Downloads/output.png";
ConvertPdfToThumbnail(pdfPath, outputPath);
Console.WriteLine("Thumbnail generated successfully!");
}
}
Issues I'm Facing: PlatformNotSupportedException when running the code:
System.PlatformNotSupportedException: Registry is not supported on this platform.
at Microsoft.Win32.RegistryKey.OpenBaseKey(RegistryHive hKey, RegistryView view)
at Ghostscript.NET.GhostscriptVersionInfo.GetInstalledVersions(GhostscriptLicense licenseType)
at Ghostscript.NET.GhostscriptVersionInfo.GetLastInstalledVersion(GhostscriptLicense licenseType, GhostscriptLicense licensePriority)
at Ghostscript.NET.Rasterizer.GhostscriptRasterizer.Open(String path)
It seems Ghostscript.NET is trying to access the Windows registry, which is not available on macOS.
'Image.Save(string, ImageFormat)' is only supported on: 'windows' 6.1 and later.
'ImageFormat.Png' is only supported on: 'windows' 6.1 and later.
I want my solution to work cross-platform (macOS and Linux).
Questions: How can I make Ghostscript.NET work on macOS without running into registry issues?
Basically How do i fix this