Paulinho Inso
Paulinho Inso

Reputation: 87

How to generate a PDF file programmatically from Xamarin mobile app

I have a rather large amount of data that I want to write to a PDF file in a table like style where each record would take a new line.

I assume that I should not be reinventing the wheel and use some sort of library. Preferably this would need to be a free or open-source library. I have tried to use PDFSharp, which seems like a perfect fit, but it gives me the error:

Package 'PDFsharp-MigraDoc 1.50.5147' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8' instead of the project target framework '.NETStandard,Version=v2.0'. This package may not be fully compatible with your project.

The question I have is: Is there another library I could use to solve my problem that would also be free? Maybe it is possible to make PDFSharp library target the correct framework version? Any tip is a good tip and I am looking forward to all of them.

Upvotes: 3

Views: 1103

Answers (1)

Ryan
Ryan

Reputation: 2570

If you are supporting Android API 19+ you can convert HTML to PDF using the OS with NO other dependencies.

So you can layout your data in HTML, and then follow this blog post on how to convert to PDF using the Android OS itself. No 3rd party libs required.

http://www.annalytics.co.uk/android/pdf/2017/04/06/Save-PDF-From-An-Android-WebView/ https://developer.android.com/training/printing/html-docs

The same can be done on iOS, you can use the OS to convert HTML to PDF. Not sure exactly which iOS versions this requires, but since the Apple print system uses PDF this should go back quite a ways in time.

Here is an example. http://aplus.rs/2018/html2pdf-renderer-for-ios/

Not sure if there is a Xamarin wrapper for the above or not.

Unless you need to support archaic iOS/Android OS the above work without 3rd party tools, and HTML is arguably the best layout engine there is anyway.

Upvotes: 1

Related Questions