Heisenberg
Heisenberg

Reputation: 31

Unable to Convert PDF to Image on macOS with .NET 8.0 C#: Tried Ghostscript, PDFium, PDFtoImage, and PdfPig—Always Get Errors

I'm working on a project where I need to convert a PDF to an image (for use as a thumbnail). I'm using .NET 8.0 with C# on macOS, and I've tried several libraries, including:

Ghostscript PDFiumViewer (pdfiumrenderer) PDFtoImage (which inherently uses PDFiumViewer and SkiaSharp) UglyToad.PdfPig However, I always encounter one of the following errors:

Error 1: Unhandled exception. System.FormatException: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters. This error occurs when using PDFtoImage. Here’s the full stack trace:

System.FormatException: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.
    at System.Convert.FromBase64CharPtr(Char* inputPtr, Int32 inputLength)
    at System.Convert.FromBase64String(String s)
    at PDFtoImage.Conversion.ToImage(String pdfAsBase64String, String password, Int32 page, RenderOptions options)
    at PDFtoImage.Conversion.SaveImpl(Stream imageStream, SKEncodedImageFormat format, String pdfAsBase64String, String password, Int32 page, RenderOptions options)
    at PDFtoImage.Conversion.SaveImpl(String imageFilename, SKEncodedImageFormat format, String pdfAsBase64String, String password, Int32 page, RenderOptions options)
    at PDFtoImage.Conversion.SavePng(String imageFilename, String pdfAsBase64String, String password, Int32 page, RenderOptions options)
    at PdfToImageApp.Program.Main(String[] args) in /Users/i42571/RiderProjects/ConsoleApp12/ConsoleApp12/Program.cs:line 18

Error 2: When calling image.Save, I get this error: Cannot resolve method 'Save(string, SixLabors.ImageSharp.Formats.Png.PngEncoder)', candidates are: ... Here's the relevant part of my code:

image.Save(outputImagePath, new SixLabors.ImageSharp.Formats.Png.PngEncoder());

I get an underline under the line with image.Save.

Error 3: When I try saving an image using System.Drawing.Imaging.ImageFormat.Png, I get this compiler warning: CA1416: This call site is reachable on all platforms. Image.Save(Stream, ImageFormat) is only supported on: 'windows' 6.1 and later.

I’m running this on macOS, and this warning prevents me from proceeding with the solution.

using System;
using PDFtoImage;

namespace PdfToImageApp
{
    class Program
    {
        static void Main(string[] args)
        {
            // Path to your PDF file
            string pdfPath = "Downloads/testconvert2.pdf";
            
            // Path to save the image
            string outputImagePath = "Downloads/output.png";
            
            // Convert the first page of the PDF to a PNG
            // Use the correct SavePng method that takes file paths as parameters
            Conversion.SavePng(pdfPath, outputImagePath);

            Console.WriteLine("PDF to Image conversion completed!");
        }
    }
}

What I’ve tried: I've tried multiple libraries (Ghostscript, PDFium, PDFtoImage, PdfPig) but nothing seems to work. I’ve verified the paths and file permissions, and I’ve tried different methods to call the SavePng function, but the errors persist.

My goal: Convert a PDF to an image, specifically for use as a thumbnail. I’m using .NET 8.0 on macOS. Can anyone help me with a solution or explain what might be causing these issues?

Upvotes: 0

Views: 70

Answers (1)

Panagiotis Kanavos
Panagiotis Kanavos

Reputation: 131712

The error says a path value was passed to a parameter that expected a BASE64-encoded string with the PDF's contents. The error shows that the parameter order is reversed too:

at PDFtoImage.Conversion.SavePng(String imageFilename, String pdfAsBase64String, String password, Int32 page, RenderOptions options)

Looking at Convert.SavePng's source code I see that no overload accepts a PDF path or FileInfo. I see overloads that accept a Stream though.

This code should work as long as there aren't other bugs. :

using(var pdfStream=File.OpenRead(pdfPath))
{
    Conversion.SavePng(outputImagePath,pdfStream,0);
}

The source code shows that SavePng expects the number of the page

public static void SavePng(string imageFilename, Stream pdfStream, Index page, bool leaveOpen = false, string? password = null, RenderOptions options = default)

Upvotes: 1

Related Questions