繁体   English   中英

如何在iPhone SDK中将NSData转换为PDF?

[英]How to convert a NSData to pdf in iPhone sdk?

正在将网页转换为pdf文件。 我做了以下事情

NSString *string=[NSString stringWithFormat:@"%@.pdf",[chapersArray objectAtIndex:pageIndex]];
[controller1 addAttachmentData:pdfData mimeType:@"application/pdf" fileName:string];
[self presentModalViewController:controller1 animated:YES];
[controller1 release];

现在如何将NSData转换为pdf并保存在应用程序内存中? 请帮助我提供示例代码或建议。 谢谢大家

假设您在此处的文档目录中有pdf文件。 您可以将其更改为实际位置。 尝试这个 -

//to convert pdf to NSData
NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:@"test.pdf"]; 
NSData *myData = [NSData dataWithContentsOfFile:pdfPath]; 

本质上,使用CGPDFDocumentCreateWithProvider可以将NSData转换为pdf,

//to convert NSData to pdf
NSData *data               = //some nsdata
CFDataRef myPDFData        = (CFDataRef)data;
CGDataProviderRef provider = CGDataProviderCreateWithCFData(myPDFData);
CGPDFDocumentRef pdf       = CGPDFDocumentCreateWithProvider(provider);

完成后,不要忘记CFRelease所有未使用的数据。

我用以下简单方法得到了它,

-(IBAction)saveasPDF:(id)sender{
     NSString *string=[NSString stringWithFormat:@"%@.pdf",[chapersArray objectAtIndex:pageIndex]];
     [controller1 addAttachmentData:pdfData mimeType:@"application/pdf" fileName:string];
     [self presentModalViewController:controller1 animated:YES];
     [pdfData writeToFile:[self getDBPathPDf:string] atomically:YES];
        }

-(NSString *) getDBPathPDf:(NSString *)PdfName {
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
   NSString *documentsDir = [paths objectAtIndex:0];
    return [documentsDir stringByAppendingPathComponent:PdfName];
}

您可以尝试使用以下解决方案:

- (void)saveDataToPDF:(NSData *)pdfDocumentData
{
    //Create the pdf document reference
    CGDataProviderRef dataProvider = CGDataProviderCreateWithCFData((CFDataRef)pdfDocumentData);
    CGPDFDocumentRef document = CGPDFDocumentCreateWithProvider(dataProvider);

    //Create the pdf context
    CGPDFPageRef page = CGPDFDocumentGetPage(document, 1); //Pages are numbered starting at 1
    CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
    CFMutableDataRef mutableData = CFDataCreateMutable(NULL, 0);

    //NSLog(@"w:%2.2f, h:%2.2f",pageRect.size.width, pageRect.size.height);
    CGDataConsumerRef dataConsumer = CGDataConsumerCreateWithCFData(mutableData);
    CGContextRef pdfContext = CGPDFContextCreate(dataConsumer, &pageRect, NULL);


    if (CGPDFDocumentGetNumberOfPages(document) > 0)
    {
        //Draw the page onto the new context
        //page = CGPDFDocumentGetPage(document, 1); //Pages are numbered starting at 1

        CGPDFContextBeginPage(pdfContext, NULL);
        CGContextDrawPDFPage(pdfContext, page);
        CGPDFContextEndPage(pdfContext);
    }
    else
    {
        NSLog(@"Failed to create the document");
    }

    CGContextRelease(pdfContext); //Release before writing data to disk.

    //Write to disk
    [(__bridge NSData *)mutableData writeToFile:@"/Users/David/Desktop/test.pdf" atomically:YES];

    //Clean up
    CGDataProviderRelease(dataProvider); //Release the data provider
    CGDataConsumerRelease(dataConsumer);
    CGPDFDocumentRelease(document);
    CFRelease(mutableData);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM