Generating a PDF with Unity ?

Hello !

I am looking to create a pdf file, with data from my Unity application. Sorry for the explanations, a bit long, but I try to give as much detail as possible to “make it easier” for you.

Before the explanations I give, I want to specify that any solution to generate a PDF with game values written in it, even completely different from what I am doing, is fine with me.

To begin with, I can already create a .csv file (Excel) with Unity, and C#'s System I/O.
However, I understood that to create a PDF file, we had to add a framework in our project.

The first framework that came up was: iText7.

So I added the plugin using the NuGet package manager.

I then saw that we had to add the .dll files, in our Unity project. I did that too.
But every time I add a dll file, Unity asks me for another one (see picture):

I have added the dll files as requested 5 times, but I have the impression that it never stops.

Finally, here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using iText.Layout;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;

public class PDF_Generator : MonoBehaviour
{
    string filename = "";

    [System.Serializable]
    public class Player
    {
        public string name;
        public int health;
        public int damage;
        public int defence;
    }

    [System.Serializable]

    public class PlayerList
    {
        public Player[] player;
    }

    public PlayerList myPlayerList = new PlayerList();
    // Start is called before the first frame update
    void Start()
    {
        filename = Application.dataPath + "/test.pdf";
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            EnglishPDF();
        }
    }

    public void EnglishPDF()
    {

        string dest = Application.dataPath + "/test.pdf";
        PdfWriter writer = new PdfWriter(dest);
        // Creating a PdfDocument
        PdfDocument pdfDoc = new PdfDocument(writer);

        // Adding an empty page
        pdfDoc.AddNewPage();

        // Creating a Document
        Document document = new Document(pdfDoc);
        // Closing the document
        document.Close();
    }
}

As you can see on this picture, there is only the Close() function that does not work.

The error message is the following:

<The type “AbstractIdentifiableElement” is defined in an assemby that is not referenced. You need to add a reference to the assembly ‘itext.commons’, Version 7.2.2.0, Culture = neutral, PublicKeyToken = (a long number)>

I work with Unity 2019.4.36f1, and Visual Studio 2019. I thank you in advance for your answers.
Thank you in advance for your answers. Sincerely.


8149520--1058522--Photo2.PNG

Unity isn’t necessarily going to digest any DLL you throw at it. It has extremely tight requirements given that it supports transcoding everything into every target, ranging from WebGL to Android to PC to Mac.

Generally your highest success will come from starting with the Unity Asset Store and searching for PDF writers. This way someone else has done all the hard work of making it operate in the Unity ecosystem.

Thank you for your reply.

It is true that for compatibility, there is nothing better than an asset from the asset store.

However, I’ve seen from searching that some people have had success (on posts from 3-4-5 years ago) with iText or iTextSharp. That’s why I’m sure there’s a way to make this plugin work with Unity.

Nevertheless, the solution of a Unity PDF asset suits me perfectly but there are about 5 of them, and they are all paying.
If someone could recommend one to avoid me spending my money randomly, that would be very nice.

While waiting for your future answers about iText / iTextSharp or a PDF asset, I’m going to poke around to see which PDF asset is best for generating a PDF with Unity.

This might help (from a Google search, fyi) https://discussions.unity.com/t/509781