2011-05-20 146 views
2

我从iPad应用程序中的UIView创建PDF。它的大小为768 * 2000。当我创建pdf时,它会创建相同的大小,并在一个页面上显示所有内容。所以我在iPad上打印时遇到问题。我使用下面的代码来创建PDF: -从UIView在iPad中创建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 path:(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  
} 

任何人都可以建议我如何缩小pdf大小,它有多个页面?

在此先感谢。

+0

你发现了解决方案吗? – 2012-04-08 17:17:22

回答

0

见例如“绘图和打印指南适用于iOS”在

https://developer.apple.com/library/ios/#documentation/2DDrawing/Conceptual/DrawingPrintingiOS/GeneratingPDF/GeneratingPDF.html#//apple_ref/doc/uid/TP40010156-CH10-SW1

基本上在清单4-1的代码示例,他们有一个do while循环,并采取通知它是如何开始一个新的PDF在循环页:

...

// Mark the beginning of a new page. 
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil); 

...

在你当前的方法中,你只调用一次begin页面方法,这就是为什么你只会有一个页面。

0

您需要为每个要创建的新PDF页面调用UIGraphicsBeginPDFPage。假设你有一个可变高度的UIView,这里是你如何在运行时将其分解成尽可能多的PDF页面:

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();