2015-03-01 3 views
1

Как создать динамические документы PDF на iOS? Я могу сделать это с помощью объектива-c/swift, но должен поддерживать динамический контент и различное количество страниц для каждого документа. Каждое создание PDF-документа должно выполняться «на лету» без использования шаблонов.Создание динамических документов PDF на iOS

+0

уверен [это поможет вам] (http://www.raywenderlich.com/6581/how-to-create-a-pdf-with-quartz-2d-in-ios-5- tutorial-part-1) & [этот поиск] (https://www.google.com.kw/webhp?sourceid=chrome-instant&rlz=1C5CHFA_enKW556KW556&ion=1&espv=2&ie=UTF-8#q=create%20pdf%20in% 20ios) –

ответ

4

Чтобы нарисовать какой-либо текст в нашем PDF-документе, нам понадобится использовать структуру основного текста. Для этого выберите цель проекта и перейдите на вкладку «Фазы построения». Нажмите знак + под опцией Link Binaries With Libraries, а затем выберите Framework CoreText.

Затем откройте ViewController.h и импортировать заголовок CoreText:

#import <CoreText/CoreText.h> 

Добавить новый метод ViewController.m, чтобы создать «привет мир» PDF.

-(void)drawText 
{ 
NSString* fileName = @"Invoice.PDF"; 

NSArray *arrayPaths = 
NSSearchPathForDirectoriesInDomains(
            NSDocumentDirectory, 
            NSUserDomainMask, 
            YES); 
NSString *path = [arrayPaths objectAtIndex:0]; 
NSString* pdfFileName = [path stringByAppendingPathComponent:fileName]; 

NSString* textToDraw = @"Hello World"; 
CFStringRef stringRef = (__bridge CFStringRef)textToDraw; 

// Prepare the text using a Core Text Framesetter. 
CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, stringRef, NULL); 
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText); 

CGRect frameRect = CGRectMake(0, 0, 300, 50); 
CGMutablePathRef framePath = CGPathCreateMutable(); 
CGPathAddRect(framePath, NULL, frameRect); 

// Get the frame that will do the rendering. 
CFRange currentRange = CFRangeMake(0, 0); 
CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL); 
CGPathRelease(framePath); 

// Create the PDF context using the default page size of 612 x 792. 
UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectZero, nil); 

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

// Get the graphics context. 
CGContextRef currentContext = UIGraphicsGetCurrentContext(); 

// Put the text matrix into a known state. This ensures 
// that no old scaling factors are left in place. 
CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity); 

// Core Text draws from the bottom-left corner up, so flip 
// the current transform prior to drawing. 
CGContextTranslateCTM(currentContext, 0, 100); 
CGContextScaleCTM(currentContext, 1.0, -1.0); 

// Draw the frame. 
CTFrameDraw(frameRef, currentContext); 

CFRelease(frameRef); 
CFRelease(stringRef); 
CFRelease(framesetter); 

// Close the PDF context and write the contents out. 
UIGraphicsEndPDFContext(); 

} 

Ссылки

1. How to create a pdf with quartz-2d in ios part-1

2. How to create a pdf with quartz 2d in ios part-2

Более подробную информацию о рамках CoreText

1. Core text tutorial for ios making a magazine app

2. About Core Text - Apple Doc

+0

есть что-нибудь свежее? что-то вроде написано быстро? – IamMashed