ios上生成pdf文档

来源:互联网 发布:聚游网络dnf登陆器 编辑:程序博客网 时间:2024/05/22 03:53

//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

}

 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectoryNSUserDomainMaskYES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"tmp.pdf"];

 

CGContextRef pdfContext = [self createPDFContext:scrolledView.bounds path:(CFStringRef)writableDBPath];

 

NSLog(@"PDF Context created");

CGContextBeginPage (pdfContext,nil); // 6

 

//turn PDF upsidedown

CGAffineTransform transform = CGAffineTransformIdentity;

transform = CGAffineTransformMakeTranslation(0scrolledView.bounds.size.height);

transform = CGAffineTransformScale(transform, 1.0, -1.0);

CGContextConcatCTM(pdfContext, transform);

 

//Draw view into PDF

[scrolledView.layer renderInContext:pdfContext];

 

CGContextEndPage (pdfContext);// 8

CGContextRelease (pdfContext);