Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Generating a PDF with Unity ?

Discussion in 'Scripting' started by Magic-Goupil, May 23, 2022.

  1. Magic-Goupil

    Magic-Goupil

    Joined:
    Mar 19, 2020
    Posts:
    8
    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:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.IO;
    5. using iText.Layout;
    6. using iText.Kernel.Geom;
    7. using iText.Kernel.Pdf;
    8.  
    9. public class PDF_Generator : MonoBehaviour
    10. {
    11.     string filename = "";
    12.  
    13.     [System.Serializable]
    14.     public class Player
    15.     {
    16.         public string name;
    17.         public int health;
    18.         public int damage;
    19.         public int defence;
    20.     }
    21.  
    22.     [System.Serializable]
    23.  
    24.     public class PlayerList
    25.     {
    26.         public Player[] player;
    27.     }
    28.  
    29.     public PlayerList myPlayerList = new PlayerList();
    30.     // Start is called before the first frame update
    31.     void Start()
    32.     {
    33.         filename = Application.dataPath + "/test.pdf";
    34.     }
    35.  
    36.     // Update is called once per frame
    37.     void Update()
    38.     {
    39.         if (Input.GetKeyDown(KeyCode.Escape))
    40.         {
    41.             EnglishPDF();
    42.         }
    43.     }
    44.  
    45.     public void EnglishPDF()
    46.     {
    47.  
    48.         string dest = Application.dataPath + "/test.pdf";
    49.         PdfWriter writer = new PdfWriter(dest);
    50.         // Creating a PdfDocument
    51.         PdfDocument pdfDoc = new PdfDocument(writer);
    52.  
    53.         // Adding an empty page
    54.         pdfDoc.AddNewPage();
    55.  
    56.         // Creating a Document
    57.         Document document = new Document(pdfDoc);
    58.         // Closing the document
    59.         document.Close();
    60.     }
    61. }
    62.  
    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.
     

    Attached Files:

  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    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.
     
  3. Magic-Goupil

    Magic-Goupil

    Joined:
    Mar 19, 2020
    Posts:
    8
    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.
     
  4. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    This might help (from a Google search, fyi) https://forum.unity.com/threads/creating-pdf-with-itextsharp-on-ios.190797/