PDFtoImage: Type initializer exception on Android

PDFtoImage version

3.0.0

OS

Android

OS version

Android 12+

Architecture

arm64

Framework

(Other)

App framework

.NET 4.6.2 (.NET standard 2.1)

Detailed bug report

Hi,

I am trying to use this lib in a very special context : Android Unity app.

We were, until now, using UWP methods to read a pdf file as a byte array. But now trying to migrate onto Android, this seemed to be the solution. In the editor, everything works great, but in the Quest 3 helmet (Android), we have this exception

The type initializer for 'PDFtoImage.PdfiumViewer.NativeMethods' threw an exception. [line: 105]

When calling this method

pageCount = Conversion.GetPageCount(documentStream, false);

with document stream :

using (UnityWebRequest www = UnityWebRequest.Get(pdfFileName))
{
				www.SendWebRequest();
				while (!www.isDone)
				{
					if (www.result == UnityWebRequest.Result.ConnectionError)
					{
						throw new Exception($"Connection Error while reading File '{pdfFileName}'");
					}
				}

				if (www.result == UnityWebRequest.Result.ConnectionError)
				{
					throw new Exception($"Connection Error while reading File '{pdfFileName}'");
				}

				pdfData = www.downloadHandler.data;

				using (var documentStream = new MemoryStream(pdfData));

(the only way to access streamingassets files on android, but this code is also called in the editor and works.)

Do you have any clue on where this is coming from ?

There is no lib or dll error prior to this, and I have the Pdfium .so for Android arm64 imported.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 18 (9 by maintainers)

Most upvoted comments

@SashelI This library can now be imported into a Unity project with the Unity Package Manager. No need to copy dll and so files around. Just follow these steps:

  1. Open your project and navigate to Window → Package Manager.
  2. Click on the + button (top-left corner) and select Add package from git URL....
  3. Enter the following URL and confirm with the Add button:
https://github.com/sungaila/PDFtoImage.git?path=etc/UnityPackage

Please let me know if this isn’t working as expected!

I just tested it and it works !! Good job.

Hey sorry for the delay, I’ll test once I finish a big projet I’m currently on.

Can’t estimate the time. Right now I am looking for a solution in SkiaSharp. Their native lib libSkiaSharp loads just fine for Mono/Unity and I wish to see how they have done that.

Thanks for posting the results!

You were right, the code executes in NativeMethods and tries to load windows kernel…

I’ll provide you with preview binaries to test once I fix this. Right now the .NET Framework binaries work on Windows only and I have to add support for other platforms.