I am creating a pdf from a UIView in my iPad Application. It has size 768 * 2000. When I create pdf then it create with same size and it shows all content on one page. So I am facing problem when I print it from iPad. i am using the following code for creating pdf :-
-(void)drawPdf:(UIView *)previewView{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"Waypoint Data.pdf"];
//CGRect tempRect = CGRectMake(0, 0, 768, 1068);
CGContextRef pdfContext = [self createPDFContext:previewView.bounds 开发者_StackOverflowpath:(CFStringRef)writableDBPath];
CGContextBeginPage (pdfContext,nil); // 6
//turn PDF upsidedown
CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformMakeTranslation(0, previewView.bounds.size.height);
transform = CGAffineTransformScale(transform, 1.0, -1.0);
CGContextConcatCTM(pdfContext, transform);
//Draw view into PDF
[previewView.layer renderInContext:pdfContext];
CGContextEndPage (pdfContext);// 8
CGContextRelease (pdfContext);
}
//Create empty PDF context on iPhone for later randering in it
-(CGContextRef) createPDFContext:(CGRect)inMediaBox path:(CFStringRef) path{
CGContextRef myOutContext = NULL;
CFURLRef url;
url = CFURLCreateWithFileSystemPath (NULL, // 1
path,
kCFURLPOSIXPathStyle,
false);
if (url != NULL) {
myOutContext = CGPDFContextCreateWithURL (url,// 2
&inMediaBox, NULL);
CFRelease(url);// 3
}
return myOutContext;// 4
}
Can anyone suggest me how can I reduce the pdf size and it has multiple page ?
Thanks in advance.
See example in the "Drawing and Printing Guide for iOS"
https://developer.apple.com/library/ios/#documentation/2DDrawing/Conceptual/DrawingPrintingiOS/GeneratingPDF/GeneratingPDF.html#//apple_ref/doc/uid/TP40010156-CH10-SW1
Basically in the Listing 4-1 code example, they have a do while loop and take notice how it starts a new PDF page in the loop:
...
// Mark the beginning of a new page.
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);
...
In your current method, you only called one of the begin page methods once so that's why you'll only ever have one page.
You need to call on UIGraphicsBeginPDFPage for every new PDF page you want to create. Assuming you have a UIView of variable height, here is how you would break it down to as many PDF pages as needed during run time:
NSInteger pageHeight = 792; // Standard page height - adjust as needed
NSInteger pageWidth = 612; // Standard page width - adjust as needed
/* CREATE PDF */
NSMutableData *pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData(pdfData, CGRectMake(0,0,pageWidth,pageHeight), nil);
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
for (int page=0; pageHeight * page < theView.frame.size.height; page++)
{
UIGraphicsBeginPDFPage();
CGContextTranslateCTM(pdfContext, 0, -pageHeight * page);
[theView.layer renderInContext:pdfContext];
}
UIGraphicsEndPDFContext();
精彩评论