最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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
  • 1 For the FormatException: Whatever you called is expecting data in form of a base64-encoded string of the pdf's content. If you just "converted" the bytes into string, then you are probably missing the base64-encoding step. – Fildor Commented Feb 5 at 13:01
  • 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:03
  • 1 The line PDFtoImage.Conversion.SavePng(String imageFilename, String pdfAsBase64String, String password, Int32 page, RenderOptions options) in the stack trace says that SavePng expects the contents of the file as BASE64, not the file path – Panagiotis Kanavos Commented Feb 5 at 13:04
  • 1 Looking at the project's source code I see that none of the SavePng methods accepts a path and the parameters are reversed. You should use the call that expects a stream instead, and load the file with using var pdfStream=File.Create(pdfPath);. The actual call should be SavePng(outputImagePath,pdfStream) – Panagiotis Kanavos Commented Feb 5 at 13:10
  • 1 Btw: Does GhostScript work if you invoke it directly from terminal? (I think that would be my go-to if I had to do it.) – Fildor Commented Feb 5 at 13:11
 |  Show 1 more comment

1 Answer 1

Reset to default 1

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)

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论