0
votes

Hi all I tried in many ways to print a pdf according with the size of the pdf book.i also searched in many links and many codes like apple codes,github and many such type of sites but no use. This question is duplicate of many questions and answers but still i didN't get exact result.my code is looking like this

  • (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
    if(UIInterfaceOrientationIsPortrait([self interfaceOrientation])){ layer.backgroundColor=[UIColor blackColor]; myPageRef = CGPDFDocumentGetPage(myDocumentRef, c);

    CGSize pageSize = [kbDataSource pageSize];
    GLogInfo(@"the page ize we are getting in draw layer is %@ ",NSStringFromCGSize(pageSize));
    //CGContextSaveGState(ctx);
    
    CGSize aspectFitSize = [self getPageFitSizeOfSize:pageSize inSize:CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height- __TOP_BRANDING_BAR_HEIGHT)];
    CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0);
    CGContextFillRect(ctx, CGContextGetClipBoundingBox(ctx));
    
    
    
    GLogDebug(@"aspect fitsize of image to %@", NSStringFromCGSize(aspectFitSize));
    NSLog(@"layer bounds are:%@  %@ ",NSStringFromCGSize(layer.bounds.size),NSStringFromCGPoint(layer.bounds.origin));
    NSLog(@"page size of the book is:%@",NSStringFromCGSize(pageSize));
    CGFloat aspectRatio=pageSize.width/pageSize.height;
        CGRect cropBox = CGPDFPageGetBoxRect(myPageRef, kCGPDFCropBox);
    CGRect targetRect = layer.bounds;
    CGFloat xScale = targetRect.size.width / cropBox.size.width;
    CGFloat yScale = (targetRect.size.height-41) / cropBox.size.height;
    CGFloat scaleToApply = (xScale < yScale) ? xScale : yScale;
    
    CGContextTranslateCTM(ctx, 0.0, -41+layer.bounds.size.height);
    CGContextScaleCTM(ctx, 1.0, -1.0);
    NSLog(@"the crop box values are %@",NSStringFromCGRect(cropBox));
    NSLog(@"the crop box values are %f",cropBox.origin.x);
    
    NSLog(@"the scaleToApply is %f",scaleToApply);
    NSLog(@"the view bounds are %@",[self.view description]);
    
    if (scaleToApply == yScale)
        CGContextConcatCTM(ctx, CGAffineTransformMakeTranslation(-100, -150));
    else
        CGContextConcatCTM(ctx, CGAffineTransformMakeTranslation(-180, -260)); 
    CGContextConcatCTM(ctx, CGAffineTransformMakeScale(scaleToApply, scaleToApply));
    CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(myPageRef, kCGPDFCropBox, layer.bounds, 0, true));
    
    
    CGContextSetInterpolationQuality(ctx, kCGInterpolationHigh); 
    CGContextSetRenderingIntent(ctx, kCGRenderingIntentDefault);
    CGContextDrawPDFPage(ctx, myPageRef);
    

    }

I pasted all the code which i tried in drawlayer. I gave many random numbers to fit the page entire screen finally i got only for Eight books(pdfs).

My goal is to print any pdf with the entire ipad screen covering. my result is getting only for few pdfs.can any one tell me the exact reason why it is happening and also tell me exact code to print the pdf to the entire screen of ipad.

thank you all.

3

3 Answers

1
votes

Pages in PDF files don't have a standard size. And since you are computing the scale to retain the correct aspect ratio, not all of your PDF files will take up the entire screen.

Based on your code above, you can make all PDF files fit the entire screen by changing

CGContextConcatCTM(ctx, CGAffineTransformMakeScale(scaleToApply, scaleToApply));

to

CGContextConcatCTM(ctx, CGAffineTransformMakeScale(xScale, yScale));

This will screw up the aspect ratio, though.

0
votes

Why not just use the UIDocumentInteractionController to display the PDF? It would be much easier then trying to render the PDF the way you are? I have used UIDocumentInteractionController on several instances to preview PDFs, word docs, powerpoints, etc.

Check it out HERE.

This may give you what you need (Fill the whole screen with the PDF). It will also allow you to print the document and open it in another app if needed...

0
votes

There is a simple way to read a PDF in iPhone/iPad: 1.Take one UIwebView (name:pdfView). 2.Give Iboutlet connection to it & Delegate it to FilesOwner 3.In Viewdidload

[self.pdfView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"ObjC" ofType:@"pdf"]]]];

4.ObjC.pdf should be in resource folder.. Now to Fit the pdf page of different size to the screen of the iPad just open respective .xib file then go to inspector of UIwebView then web view attributes and there tick mark scales page to fit option.