PDF rendering on Apple TV tvOS

I am working on an add-on to my tvOS application that will allow you to view PDF files stored in the application. However, without UIWebView, I don’t understand how to do this. I asked the question elsewhere and welcome the link to Apple's verbose and helpless API document that can be used, and even here it is mentioned (CGPDFPage), but there is no real guide on how to implement this, Someone successfully did it on tvOS, and if so, could you help me get started in this process?

+7
pdf tvos
source share
2 answers

Below is the code I wrote and tested on tvOS. Note that this is in Objective-c.

I created two functions for completing the task and one helper function for displaying PDF images in UIScrollView. The first will open the PDF from the URL. The web address was used. You can also use a local file in this example.

There is also a helper function to open a document from a local file.

The second function turns a PDF document into context. I decided to display the context by creating an image from it. There are other ways to handle context.

Opening a document is pretty straight forward, so there are no comments in the code for this. Rendering a document is somewhat more active, so there are comments explaining this feature.

Below is the full application.

- (CGPDFDocumentRef)openPDFLocal:(NSString *)pdfURL { NSURL* NSUrl = [NSURL fileURLWithPath:pdfURL]; return [self openPDF:NSUrl]; } - (CGPDFDocumentRef)openPDFURL:(NSString *)pdfURL { NSURL* NSUrl= [NSURL URLWithString:pdfURL]; return [self openPDF:NSUrl]; } - (CGPDFDocumentRef)openPDF:(NSURL*)NSUrl { CFURLRef url = (CFURLRef)CFBridgingRetain(NSUrl); CGPDFDocumentRef myDocument; myDocument = CGPDFDocumentCreateWithURL(url); if (myDocument == NULL) { NSLog(@"can't open %@", NSUrl); CFRelease (url); return nil; } CFRelease (url); if (CGPDFDocumentGetNumberOfPages(myDocument) == 0) { CGPDFDocumentRelease(myDocument); return nil; } return myDocument; } - (void)drawDocument:(CGPDFDocumentRef)pdfDocument { // Get the total number of pages for the whole PDF document int totalPages= (int)CGPDFDocumentGetNumberOfPages(pdfDocument); NSMutableArray *pageImages = [[NSMutableArray alloc] init]; // Iterate through the pages and add each page image to an array for (int i = 1; i <= totalPages; i++) { // Get the first page of the PDF document CGPDFPageRef page = CGPDFDocumentGetPage(pdfDocument, i); CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox); // Begin the image context with the page size // Also get the grapgics context that we will draw to UIGraphicsBeginImageContext(pageRect.size); CGContextRef context = UIGraphicsGetCurrentContext(); // Rotate the page, so it displays correctly CGContextTranslateCTM(context, 0.0, pageRect.size.height); CGContextScaleCTM(context, 1.0, -1.0); CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(page, kCGPDFMediaBox, pageRect, 0, true)); // Draw to the graphics context CGContextDrawPDFPage(context, page); // Get an image of the graphics context UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); [pageImages addObject:image]; } // Set the image of the PDF to the current view [self addImagesToScrollView:pageImages]; } -(void)addImagesToScrollView:(NSMutableArray*)imageArray { int heigth = 0; for (UIImage *image in imageArray) { UIImageView *imgView = [[UIImageView alloc] initWithImage:image]; imgView.frame=CGRectMake(0, heigth, imgView.frame.size.width, imgView.frame.size.height); [_scrollView addSubview:imgView]; heigth += imgView.frame.size.height; } } 

And to tie it all together, you can do this:

 CGPDFDocumentRef pdfDocument = [self openPDFURL:@"http://www.guardiansuk.com/uploads/accreditation/10testing.pdf"]; [self drawDocument:pdfDocument]; 

Please note that I am using a random PDF file that was available for free on the Internet. I ran into some problems with https urls, but I'm sure this can be resolved, and this is not actually related to the issue of opening a PDF.

+5
source share

The tvOS documentation contains a section for creating, viewing and converting PDF documents , so I believe that it contains the necessary functions.

Theres a lot of sample code on this page, but there is code that I use for iOS for the same purpose. It should work on tvOS, but I have no way to test it:

 func imageForPDF(URL: NSURL, pageNumber: Int, imageWidth: CGFloat) -> UIImage { let document = CGPDFDocumentCreateWithURL(URL) let page = CGPDFDocumentGetPage(document, pageNumber) var pageRect = CGPDFPageGetBoxRect(page, .MediaBox) let scale = imageWidth / pageRect.size.width pageRect.size = CGSizeMake(pageRect.size.width * scale, pageRect.size.height * scale) pageRect.origin = CGPointZero UIGraphicsBeginImageContext(pageRect.size) let ctx = UIGraphicsGetCurrentContext() CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0) // White background CGContextFillRect(ctx, pageRect) CGContextSaveGState(ctx) // Rotate the PDF so that it's the right way around CGContextTranslateCTM(ctx, 0.0, pageRect.size.height) CGContextScaleCTM(ctx, 1.0, -1.0) CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(page, .MediaBox, pageRect, 0, true)) CGContextDrawPDFPage(ctx, page) CGContextRestoreGState(ctx) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image } 
0
source share

All Articles