Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

SharpPDF PDF runtime generator

Discussion in 'Formats & External Tools' started by aesparza, Nov 22, 2016.

  1. aesparza

    aesparza

    Joined:
    Apr 4, 2016
    Posts:
    29
    Hi!
    I am using SharpPDF to runtime PDF generation. I'm using it on Windows. I found two problems:

    • I need to put an scaled image on a page. I'm using addImage.
    It doesn't scale the image but it cuts the image starting from upper-left corner with the size I would like to scale it to.
    How can I solve this? In MAC doesn't happen that.
    • I have a problem adding images to a PDF file with WINDOWS. The PDF doesnt show the images well (they are grated) when I open it with ADOBE READER. But, if I open it with another pdf reader, the images are well. How can I solve this problem? On the contrary, if I use SharpPDF in MAC, this problem doesnt exist, I need to use it on Windows.
    I have post both questions in the page of the lib inside SourceForge but I didn't get any answer.

    Could anybody help me with this please?
     
  2. burcinpak

    burcinpak

    Joined:
    Mar 6, 2018
    Posts:
    1
    Did you find a solution to this? I have the same problem. If I open it with another viewer and save as pdf again, acrobat opens it fine.
     
  3. aciusa

    aciusa

    Joined:
    Apr 19, 2016
    Posts:
    9
    if you are using the public unity conversion of sharp PDF then you need to make use of the new height field on Windows, in imageElement. This is read in getText when the PDF encoding takes place.


    internal imageElement(Byte[] myImage, int newCoordX, int newCoordY, int imageHeight, int imageWidth, int newImageHeight, int newImageWidth) //added newImg H/W to constructor
    {

    try
    {
    //_content = new byte[myImage.Length];
    _content = myImage;
    _height = imageHeight;
    _width = imageWidth;
    _newHeight = newImageHeight; //new code
    _newWidth = newImageWidth; //new code
    _coordX = newCoordX;
    _coordY = newCoordY;
    }
    catch (System.IO.FileNotFoundException ex)
    {
    throw new pdfImageNotFoundException("Immagine non trovata!", ex);
    }
    catch (System.IO.IOException ex)
    {
    throw new pdfImageIOException("Errore generale di IO sull'immagine ", ex);
    } finally {
    // if (tmpContent != null) {
    // tmpContent.Dispose();
    // tmpContent = null;
    // }
    // if (outStream != null) {
    // outStream.Close();
    // outStream = null;
    // }
    }
    }
     
  4. MerkSim

    MerkSim

    Joined:
    Mar 1, 2018
    Posts:
    12
    Great, Exactly what I was looking for!
    But where do you get the 2 extra values that you pass as arguments?
    thanks!
     
  5. aciusa

    aciusa

    Joined:
    Apr 19, 2016
    Posts:
    9
    The imageHeight and imageWidth are the pixel height and width of the source image, newImageHeight and newImageWidth are the target height of the image. The standard for relative size is 72 pixels per inch, so a standard sheet of paper is 612 X 792 (8.5* 72 X 11 * 72). Say you wanted the image to take up exactly 1/4 of the page, you would set newImageHeight to 396 (11/2*72) and newImageWidth to 306 (8.5/2*72). Sharp PDF does not account for margins, so I recommend calculating a 54 pixel margin (3/4 inch) when you are creating the algorithm to position your export elements.
     
    Last edited: Sep 14, 2018
  6. MerkSim

    MerkSim

    Joined:
    Mar 1, 2018
    Posts:
    12
    I used the same i.e imageHeight=newImageHeight , imageWidth=newImageWidth, because I wanted to have a high PPI (400) A4 page end result , so my image is 3072 by 3072. The result is the same as before: it still only opens in edge, while in chrome its completely blank and in acrobat reader it says "There was an error while reading this Stream"
     
  7. aciusa

    aciusa

    Joined:
    Apr 19, 2016
    Posts:
    9
    Sorry I have been busy with work and wasn't watching my forum alerts very diligently.

    First its important that you understand that imageHeight != newImageHeight and imageWidth != newImageWidth, if you read through the code above, you will see that I am setting _newHeight = newImageHeight and _newWidth = newImageWidth. In the poorly named getText() export function inside imageElement.cs, _newHeight and _newWidth get passed as the end size if they are greater than 0 while _height and _width always get passed as the source image size. By setting _height = newImageHeight you are declaring newImageHeight to be the original size of your source image instead of the new size you want the image to be displayed at, so no resizing takes place, your image will just be clipped outside the size you declared. An A4 page's maximum dimensions are 595w x 841h (cm -> in * 72), if you are passing 3072 as your new height/width then of course it will not process properly. Instead set _height and _width to be your 3072 then set newHeight and newWidth to be 552 (3072/400*72 assuming consistent dimensions) which will give you exactly 400 dpi over a 7.68 in square area.
     
  8. MerkSim

    MerkSim

    Joined:
    Mar 1, 2018
    Posts:
    12
    Hey, no problem I was also away!

    I had also modified the following part (of a PDFPage definition) to start with a way bigger page, but I also changed it back now.

    Code (CSharp):
    1. public pdfPage()
    2.         {
    3.             //taking 3072 pixel pics
    4.             //_height = 4677;
    5.             //_width = 3307;
    6.             //A4 at 400 ppi
    7.  
    8.             //old values
    9.  
    10.             _height = 792;
    11.             _width = 612;
    12.  
    13.             _elements = new ArrayList();
    14.         }
    Whether I pass the suggested values or 3072 to


    Code (CSharp):
    1. _newHeight = newImageHeight; //new code
    2. _newWidth = newImageWidth; //new code
    Here's the bigger picture, following the sample code from http://www.francescogallorini.com/2011/02/unity-sharp-pdf/

    Code (CSharp):
    1. path = Path.Combine(Application.persistentDataPath, "data");
    2.  
    3. string attacName = "info.pdf";
    4.  
    5. StartCoroutine(CreatePDF());
    6.  
    7. public IEnumerator CreatePDF()
    8.     {
    9.         List<Texture2D> tempCopy = new List<Texture2D>(generatedPages);
    10.  
    11.         int counter = 0;
    12.         myDoc = new pdfDocument("Info", "Name", false);
    13.  
    14.         foreach (var item in tempCopy)
    15.         {
    16.             pdfPage page = myDoc.addPage();
    17.             Debug.Log(Path.Combine(path, counter.ToString() + ".png"));
    18.  
    19.             yield return StartCoroutine(page.newAddImage(Path.Combine(path, counter.ToString() + ".png"), 0, 0));
    20.             counter++;
    21.         }
    22.         myDoc.createPDF(Path.Combine(path, attacName));
    All pictures are saved before, in the persistent data path, under a "data" folder and named with increasing numbers:
    "0.png" , " 1.png", etc...

    Maybe it helps:
    -It opens in edge (shows content normally)
    -Blank page in Chrome
    -Error reading stream on acrobat reader

    -Intended for use with iOS BUT the behavior is the same when I run it on my desktop as well.
     
  9. aciusa

    aciusa

    Joined:
    Apr 19, 2016
    Posts:
    9
    Could you post your pdfPage.newAddImage and pdfPage.AddImage functions? it doesn't look like you are passing new height width values in your initial call or you are overwriting your height/width with the initial call.
     
  10. Mad_26

    Mad_26

    Joined:
    Apr 25, 2019
    Posts:
    15
    Hello , I am not able to add the image in the pdf. I am using sharp pdf plugin with unity3d .Can u help me out with code or tips.
     
    Last edited: May 14, 2019
  11. Max-Dep

    Max-Dep

    Joined:
    Jul 15, 2012
    Posts:
    2
    Hi

    This AddImage is driving me crazy too.
    I have the same requirements as Merksim : reasonably High DP(not necessarily 400Dpi though)
    I followed Aciusia's reco and modified the imageElement accordingly.
    After what I used the following code to add images to the pdf pages

    Code (CSharp):
    1.  
    2.  
    3. byte[]img;
    4.  
    5. //After generating a 2970*2100 img, I add it as an image to the current page
    6. currentPage.addImage(img, 0, 0, 2100, 2970, mmToPoints(210), mmToPoints(297));
    7.  
    8. // I Modified the AddImage to be able to set the new Width and new Height.(Note: I use a utility to convert mm to Points(the physical unit. not the pixels)
    9.  
    10.     public void addImage(Byte[] myImage, int X, int Y, int imageHeight, int imageWidth, int newHeight, int newWidth)
    11.         {
    12.             try
    13.             {
    14.                 imageElement objImage = new imageElement(myImage, X, Y, imageHeight, imageWidth,newHeight,newWidth);
    15.                 _elements.Add(objImage);
    16.                 objImage = null;
    17.             }
    18.             catch (pdfImageNotFoundException ex)
    19.             {
    20.                 throw new pdfImageNotFoundException(ex.Message, ex);
    21.             }
    22.             catch (pdfImageIOException ex)
    23.             {
    24.                 throw new pdfImageIOException(ex.Message, ex);
    25.             }
    26.             Debug.Log("addedIMage");
    27.         }
    28.  
    29.  
    30.  

    The result pretty bad : Image resolution is pretty low... and of course it doesn't read in Acrobat. It works in preview on my mac tough.

    Strangely I get better result when I do that:

    Code (CSharp):
    1.  
    2. byte[]img;
    3.  
    4. currentPage.addImage(img, 0, 0, mmToPoints(210), mmToPoints(297));
    5.  
    6.     public void addImage(Byte[] myImage, int X, int Y, int imageHeight, int imageWidth)
    7.         {
    8.             try
    9.             {
    10.                 imageElement objImage = new imageElement(myImage, X, Y, imageHeight, imageWidth,imageHeight,imageWidth);
    11.                 _elements.Add(objImage);
    12.                 objImage = null;
    13.             }
    14.             catch (pdfImageNotFoundException ex)
    15.             {
    16.                 throw new pdfImageNotFoundException(ex.Message, ex);
    17.             }
    18.             catch (pdfImageIOException ex)
    19.             {
    20.                 throw new pdfImageIOException(ex.Message, ex);
    21.             }
    22.             Debug.Log("addedIMage");
    23.         }
    24.  
    25.  
    26.  
    The resolution is still pretty bad but I can open it in Acrobat...

    @Everyone Any idea?
     
    Last edited: Sep 4, 2020