Search Unity

  1. Unity 2019.1 beta is now available.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. On February 28th the Feedback website will shut down and be redirected to the Unity forums. See the full post for more information.
    Dismiss Notice
  5. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  6. Unity 2018.3 is now released.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

SharpPDF PDF runtime generator

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

  1. aesparza

    aesparza

    Joined:
    Apr 4, 2016
    Posts:
    28
    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:
    6
    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:
    10
    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:
    6
    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:
    10
    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:
    6
    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:
    10
    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:
    6
    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.