$ cat ~/.bash_profile alias ios_builder="/Users/peinguin/Library/Application\ Support/Titanium/mobilesdk/osx/3.1.3.GA/iphone/builder.py" alias titanium.py="/Users/peinguin/Library/Application\ Support/Titanium/mobilesdk/osx/3.1.3.GA/titanium.py"
$ titanium.py create --platform=iphone --type=module --dir=/Volumes/yanpix_projects --name=pdfsaver --id=ti.pdfsaver
$ ./build.py
// TODO: write your module tests here var pdfsaver = require('ti.pdfsaver'); Ti.API.info("module is => " + pdfsaver); var old = Titanium.Filesystem.getFile(Titanium.Filesystem.getTempDirectory(),'test.pdf'); var newpdf = Titanium.Filesystem.getFile(Titanium.Filesystem.getTempDirectory(),'export.pdf'); pdfsaver.saveInExportFileWithDrawings( old.resolve(), newpdf.resolve(), { 1: 'data:image/png;base64,[base64 image representation], 4: 'data:image/png;base64,[base64 image representation]' }, 1 ); var jpeg = Titanium.Filesystem.getFile(Titanium.Filesystem.getTempDirectory(),'export.jpeg'); pdfsaver.saveThumbnail( newpdf.resolve(), jpeg.resolve() );
#pragma Public APIs -(void)saveThumbnail:(id)args{ NSString *pdf = [args objectAtIndex:0]; NSString *jpeg = [args objectAtIndex:1]; CFURLRef url = CFURLCreateWithFileSystemPath (NULL, (CFStringRef)pdf, kCFURLPOSIXPathStyle, 0); CGPDFDocumentRef templateDocument = CGPDFDocumentCreateWithURL(url); CFRelease(url); CGPDFPageRef templatePage = CGPDFDocumentGetPage(templateDocument, 1); // get the first page CGRect templatePageBounds = CGPDFPageGetBoxRect(templatePage, kCGPDFCropBox); UIGraphicsBeginImageContext(templatePageBounds.size); CGContextRef contextRef = UIGraphicsGetCurrentContext(); CGContextTranslateCTM(contextRef, 0.0, templatePageBounds.size.height); CGContextScaleCTM(contextRef, 1.0, -1.0); CGContextDrawPDFPage(contextRef, templatePage); UIImage *imageToReturn = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); CGPDFDocumentRelease(templateDocument); [UIImageJPEGRepresentation(imageToReturn, 1.0) writeToFile:jpeg atomically:YES]; } -(void)saveInExportFileWithDrawings:(id)args{ NSString *fresh = [args objectAtIndex:0]; NSString *exportpath = [args objectAtIndex:1]; NSDictionary *drawings = [args objectAtIndex:2]; NSNumber *all = [args objectAtIndex:3]; CFURLRef url = CFURLCreateWithFileSystemPath (NULL, (CFStringRef)fresh, kCFURLPOSIXPathStyle, 0); CGPDFDocumentRef templateDocument = CGPDFDocumentCreateWithURL(url); CFRelease(url); size_t count = CGPDFDocumentGetNumberOfPages(templateDocument); UIGraphicsBeginPDFContextToFile(exportpath, CGRectMake(0, 0, 612, 792), nil); for (int pageNumber = 1; pageNumber <= count; pageNumber++) { id image = [drawings objectForKey:[NSString stringWithFormat:@"%d",pageNumber ]]; if(image == nil && [all boolValue] == NO){ continue; } CGPDFPageRef templatePage = CGPDFDocumentGetPage(templateDocument, pageNumber); CGRect templatePageBounds = CGPDFPageGetBoxRect(templatePage, kCGPDFCropBox); UIGraphicsBeginPDFPageWithInfo(templatePageBounds, nil); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextTranslateCTM(context, 0.0, templatePageBounds.size.height); CGContextScaleCTM(context, 1.0, -1.0); CGContextDrawPDFPage(context, templatePage); CGContextTranslateCTM(context, 0.0, templatePageBounds.size.height); CGContextScaleCTM(context, 1.0, -1.0); if(image != nil){ NSURL *url = [NSURL URLWithString:image]; NSData *imageData = [NSData dataWithContentsOfURL:url]; UIImage *ret = [UIImage imageWithData:imageData]; [ret drawInRect:CGRectMake(0, 0, templatePageBounds.size.width, templatePageBounds.size.height)]; } } CGPDFDocumentRelease(templateDocument); UIGraphicsEndPDFContext(); }
$ titanium.py run
<modules> <module platform="iphone">ti.pdfsaver</module> </modules>
Source: https://habr.com/ru/post/201354/
All Articles