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?

Oleg Danu
  • 4,149
  • 4
  • 29
  • 47

1 Answers1

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.

Reed Olsen
  • 9,099
  • 4
  • 37
  • 47
viggio24
  • 12,316
  • 5
  • 41
  • 34
  • 1
    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. – Lee Probert Sep 29 '11 at 20:35
  • 1
    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. – viggio24 Sep 29 '11 at 21:02
  • 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. – Mayur Birari Mar 30 '12 at 12:51