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?
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?
Share Improve this question asked Feb 5 at 12:54 HeisenbergHeisenberg 293 bronze badges 6 | Show 1 more comment1 Answer
Reset to default 1The 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)
CA1416: This call site is reachable on all platforms. Image.Save(Stream, ImageFormat) is only supported on: 'windows' 6.1 and later.
- Well, it tells you that you cannot do this on Linux and Mac but only on windows. So, whatever causes it, you need to find a Mac-Specific or Platform-Agnostic alternative. – Fildor Commented Feb 5 at 13:03PDFtoImage.Conversion.SavePng(String imageFilename, String pdfAsBase64String, String password, Int32 page, RenderOptions options)
in the stack trace says thatSavePng
expects the contents of the file as BASE64, not the file path – Panagiotis Kanavos Commented Feb 5 at 13:04SavePng
methods accepts a path and the parameters are reversed. You should use the call that expects a stream instead, and load the file withusing var pdfStream=File.Create(pdfPath);
. The actual call should beSavePng(outputImagePath,pdfStream)
– Panagiotis Kanavos Commented Feb 5 at 13:10