0

I am trying to save the PDFs of all the graphs generated by Core Plot into a single pdf file. Hence I am iterating over my graphs array and appending the NSData pdfforeach graph to pdfData and in the end writing the Pdfdata to a file. However only the pdf of the last graph in the graph array appears in the written file. Am I missing something here.

Code Snippet:

NSMutableData *pdfData = [[NSMutableData alloc] init];

for (CPTGraph *eachGraph in _graphsArray) {

 NSData *pdfDataForGraph = [eachGraph dataForPDFRepresentationOfLayer];

if(pdfDataForGraph)

   {
 [pdfData appendData:pdfDataForGraph];
   }

  }


if(0 < pdfData.length)
{

NSLog(@"Content Present");

 NSURL *documentPathUrl = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];

NSString *pdfSaved = "example.pdf";

NSURL *pdfUrl = [documentPathUrl URLByAppendingPathComponent:pdfSaved];

[pdfData writeToURL:pdfUrl atomically:NO];


 }
SSS
  • 119
  • 9

1 Answers1

0

You can't just merge the data blocks--that's not how PDFs work. Use PDFKit and add each new page to a new output document. There are several Q&As on StackOverflow with code, e.g., this one: Merging PDF Files in Cocoa

Eric Skroch
  • 27,381
  • 3
  • 29
  • 36