CGPDFDocumentRef from NSData
Asked Answered
I

1

14

I get my PDF from SQLite DB into a NSData variable. Now what are my options to create CGPDFDocumentRef from this NSData?

Or what are my options anyway to create this CGPDFDocumentRef, have the data in SQLite?

Ironwood answered 2/2, 2011 at 14:35 Comment(0)
T
37

You can create a PDF document using this function:

CGPDFDocumentRef CGPDFDocumentCreateWithProvider (
   CGDataProviderRef provider
);

To create the provider you can use this function:

CGDataProviderRef CGDataProviderCreateWithCFData (
   CFDataRef data
);

and consider that NSData and CFDataRef are toll-free bridged so you can use them interchangeably.

So summarizing try this:

NSData *data = ... my data from SQLite ...
CFDataRef myPDFData = (CFDataRef)data;
CGDataProviderRef provider = CGDataProviderCreateWithCFData(myPDFData);
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithProvider(provider);

Don't forget to CFRelease all unused data.

Trio answered 2/2, 2011 at 14:49 Comment(3)
do you know how I can write that PDF to a file in ios after creating it. I tried writeToFile from the NSData object but I can't see the content in the PDF. The data is created from a CGPath.Heraldic
UIGraphicsBeginPDFContextToFile(...) creates the PDF file, then you create pages using UIGraphicsBeginPDFPage() by writing graphics using your CGPath, and finally close the file with UIGraphicsEndPDFContext(). The result will be the saved PDF.Trio
I have hard luck of converting NSData into pdf, actually i want to show that NSData on webView but i always got this error- "failed to find PDF header: `%PDF' not found.", any help will be crucial.Starlet

© 2022 - 2024 — McMap. All rights reserved.