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

Displaying a PDF in a Unity App.

Discussion in 'Editor & General Support' started by techmage, Jun 10, 2011.

  1. techmage

    techmage

    Joined:
    Oct 31, 2009
    Posts:
    2,133
    So the company I'm developing a Unity app for asked me to implement a feature that no Unity developer should have to deal with implementing. They asked me to display a PDF inside the unity app. Ahhhh, not that, anything but that!

    Anyways, I've got a working solution on iOS by using iOS's webView GUI component in xCode to display the PDF. I've got it setup to work nicely where you hit a button in Unity and it draws the Xcode GUI over the top of unity to display the pdf file in the WebView.

    But when it comes to Mac and PC builds I'm kind of struggling here. I have already thoroughly explored both Berkelium and Awesomium Unity wrappers to see if they will render PDF files, they don't. I found the htmlTexture plugin for mac, but am hesitant to go that route because I can't use it on Windows. I would like to use the same solution on win and mac (and ideally iOS if I could find a way).

    I have explored the available C# pdf readers that can render the PDF to a bitmap or texture data for displaying in Unity and the ones I tried didn't seem to work with MonoDevelop, or had really high licensing fees so I can't consider them.

    I really want to avoid just exporting the PDF as images and displaying an array of images... but thats looking like it might be the only option on PC and Mac... Has anyone had to do this, or have any ideas on how it might be possible?

    If anyone happens to come across this post in a few months/years, I did find one possible solution in library called 'clownPDF' seems to be mono compatible and like you could assemble a DLL that could run in Unity. Only issue is the function in it to render PDF's to bitmap is 'alpha' and just simply doesn't work on my end at all... Maybe in a year the author will make it work and then you could render PDF's to a texture inside Unity, but for now I need something else.
     
    Last edited: Jun 10, 2011
  2. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    Over thinking this .. take this app for instance:
    http://www.intrapdf.com/convert_pdf_to_html.htm

    One of many actually but google up tools that you can buy and run from command line for either Mac / PC or through an API that converts a PDF to HTML or better yet, JPG images and simply use those in Unity on a plane or what ever fits your fancy. Forget those that do bitmaps, *cringes*, I can't remember the tool I purchased for work but it does the same thing for me, I simply drop PDF's generated from Crystal Reports on the crystal server down to a network share, then I have a program that runs against that share on the network that converts all PDF's generated into web pages and jpgs's, then I simply pull those in to my web or other programs I have written.

    All the same concept really.
     
  3. techmage

    techmage

    Joined:
    Oct 31, 2009
    Posts:
    2,133
    Ive decided to just go with a string of images instead of a PDF file. It would be cool to use an HTML file like you say but is there any solution that will work simoultaneously on iOS, PC and Mac? I explored this route myself but was hesitant to go that way because I'd have to use the xCode webview on iOS, htmlTexture on Mac and Berkelium on PC... which would be alot of work. Is there some other solution I've missed that would work on all platforms?
     
  4. cslanzi76

    cslanzi76

    Joined:
    Aug 17, 2011
    Posts:
    1
    I got into the same problem. on iPhone I'm using native components, a library for pdf.. on Pc/Mac Standalone I use Application.OpenURL and I provide a file:// url and it opens the pdf with a standard application, not inside the unity app but still a nice solution in my case
     
  5. techmage

    techmage

    Joined:
    Oct 31, 2009
    Posts:
    2,133
    I figure I should post this back here. After ALOT of struggling with various issues. I extended Prime31's etcetera plugin to be able to launch this pdf viewer library https://github.com/vfr/Reader on iOS

    It works really well. Before this I had never touched xCode before and it only took me two days to figure out how to alter Prime31's plugin and that pdf library to get it working how I wanted. So it's not really diffucult if you have a good grasp on general programming concepts. I can't post code though because it is a company project.
     
  6. Wolfos

    Wolfos

    Joined:
    Mar 17, 2011
    Posts:
    950
    Convert to TIFF (if you're using a Mac, just use Preview) and make it a large texture.
     
  7. Berrington

    Berrington

    Joined:
    May 26, 2010
    Posts:
    13
    eem, Does this solution also work on your PC?
     
  8. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    @eem: Sounds damm good , my problem I'm a null on objective C.

    What is very important for me to know before taking a deeper look on this is, how did you use the library in your project?

    Is it possible to convert the pages on runtime to a Texture like needed or how does this work?
     
    Last edited: Oct 17, 2011
  9. techmage

    techmage

    Joined:
    Oct 31, 2009
    Posts:
    2,133
    I also can't post full code because it's partially mixed with Prime31's etcetera plugin, and you would need to purchase the plugin from Prime31 in order to get the full code. But I will show you the method I changed in the etcetera plugin

    Code (csharp):
    1.  
    2. // P31WebController
    3. - (void)showWebControllerWithUrl:(NSString*)url
    4. {
    5.     UnityPause( true );
    6.  
    7.    
    8.     ReaderDemoController *reader = [[ReaderDemoController alloc]  initWithNibName:nil bundle:nil];      
    9.     reader.path = url;
    10.    
    11.     UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:reader];
    12.    
    13.     navCon.navigationBar.barStyle = UIBarStyleBlack; navCon.navigationBar.translucent = YES;
    14.    
    15.     [self showViewControllerModallyInWrapper:navCon];
    16.    
    17.     [navCon release];
    18.     [reader release];
    19.    
    20.     //ORIGINAL CODE IN FUNCTION
    21.     //P31WebController *webCon = [[P31WebController alloc] initWithUrl:url];
    22.     //UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:webCon];
    23.     //[self showViewControllerModallyInWrapper:navCon];
    24.     //[navCon release];
    25.     //[webCon release];
    26. }
    I changed the function of the etcetera plugin that calls up the webview controller to instead call up the pdf library.

    Add the reader project to your xcode project, and then you can quite literally just exchange the instance of WebController for an instance of ReaderDemoController class and then pass that to the navigation controller and pop it up modally.

    There is some more editing that you need to do to the class to get it to work how you want. Like for example I added a variable to the class so I could do the reader.path = url and set the pdf file it opens on from that function.

    You will then also have to look through pdf library and have it call Prime31's dissmissModalView command when you click 'done' in order to close the pdf library. If you buy prime31's plugin and can't find this feel free to ask, I'll point it out to you in a PM.

    I must however forewarn you there is one issue that may or not be big. In order to get the pdf reader working properly I had to disable CADDisplayLink and enable the NSTimer.
    I describe the solution to this problem here:
    http://forum.unity3d.com/threads/100066-UIScrollView-bugs-out-when-drawn-over-Unity.

    However Dreamora, who is considerably more knowloedgable than me on this subject, says that this is a bad idea. It may have some impacts on performance which I have not thoroughly investigated. But for my app, which was not performance critical, this worked just fine.
     
    Last edited: Oct 19, 2011
  10. Plow-Digital

    Plow-Digital

    Joined:
    Mar 5, 2014
    Posts:
    6
    Techmage,

    We are looking for a developer to expand the PDF support for Unity projects... such page turn support, scrubbing thumbnails pages... -Greg
     
  11. ignusmart

    ignusmart

    Joined:
    Dec 14, 2014
    Posts:
    2
    Excuse me,I'm new in unity, someone can found the solution to this issue? (Sorry for my poor english)
     
  12. jaydeep01

    jaydeep01

    Joined:
    May 22, 2018
    Posts:
    7
    Does any one fixed this issue? I am working on converting PDF to image for Window, MAC and Linux. Please help.
     
  13. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    For everyone looking to integrate PDF support, there are some very nice plugins in the Assetstore that do have good multi platform support for rendering PDF searching and thumbnails. Converting the PDFs to a image file is a option but it will increase the needed disk space by a lot.
    My favorite one is this one https://assetstore.unity.com/packages/tools/gui/pdf-renderer-32815
    Yes its not free but for what you get is definitively worth the money.