2011-09-12 185 views
2

我有一個很大的麻煩。我想合併多個PDF文件在iphone應用程序的一個PDF文件。但是我不能。你可以幫我嗎 ?非常感謝如何將多個PDF文件合併爲一個PDF

+0

您想使用現有的應用多個PDF文件合併成一個PDF文件,或者你想創建一個新的應用程序嗎? –

+1

使用['pdftk'](http://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/),'cat'命令。 – Orbling

+0

@Orbling:在iOS上,嗯。 :/ – BastiBen

回答

3

如果我正確地理解了這個問題,您正試圖將一堆獨立的PDF文件添加到一個PDF文件中。

我用下面的代碼將1個pdf文件添加到另一個pdf文件的末尾來創建一個pdf文件。

//NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *cacheDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; 

// File paths 
NSString *pdfPath1 = [[NSBundle mainBundle] pathForResource:@"pdf1" ofType:@"pdf"]; 
NSString *pdfPath2 = [cacheDir stringByAppendingPathComponent:@"temp.pdf"]; 
NSString *pdfPathOutput = [cacheDir stringByAppendingPathComponent:@"out.pdf"]; 

// File URLs - bridge casting for ARC 
CFURLRef pdfURL1 = (__bridge_retained CFURLRef)[[NSURL alloc] initFileURLWithPath:(NSString *)pdfPath1];//(CFURLRef) NSURL 
CFURLRef pdfURL2 = (__bridge_retained CFURLRef)[[NSURL alloc] initFileURLWithPath:(NSString *)pdfPath2];//(CFURLRef) 
CFURLRef pdfURLOutput =(__bridge_retained CFURLRef) [[NSURL alloc] initFileURLWithPath:(NSString *)pdfPathOutput];//(CFURLRef) 

// File references 
CGPDFDocumentRef pdfRef1 = CGPDFDocumentCreateWithURL((CFURLRef) pdfURL1); 
CGPDFDocumentRef pdfRef2 = CGPDFDocumentCreateWithURL((CFURLRef) pdfURL2); 

// Number of pages 
NSInteger numberOfPages1 = CGPDFDocumentGetNumberOfPages(pdfRef1); 
NSInteger numberOfPages2 = CGPDFDocumentGetNumberOfPages(pdfRef2); 

// Create the output context 
CGContextRef writeContext = CGPDFContextCreateWithURL(pdfURLOutput, NULL, NULL); 

// Loop variables 
CGPDFPageRef page; 
CGRect mediaBox; 

// Read the first PDF and generate the output pages 
NSLog(@"GENERATING PAGES FROM PDF 1 (%i)...", numberOfPages1); 
for (int i=1; i<=numberOfPages1; i++) { 
    page = CGPDFDocumentGetPage(pdfRef1, i); 
    mediaBox = CGPDFPageGetBoxRect(page, kCGPDFMediaBox); 
    CGContextBeginPage(writeContext, &mediaBox); 
    CGContextDrawPDFPage(writeContext, page); 
    CGContextEndPage(writeContext); 
} 

// Read the second PDF and generate the output pages 
NSLog(@"GENERATING PAGES FROM PDF 2 (%i)...", numberOfPages2); 
for (int i=1; i<=numberOfPages2; i++) { 
    page = CGPDFDocumentGetPage(pdfRef2, i); 
    mediaBox = CGPDFPageGetBoxRect(page, kCGPDFMediaBox); 
    CGContextBeginPage(writeContext, &mediaBox); 
    CGContextDrawPDFPage(writeContext, page); 
    CGContextEndPage(writeContext);  
} 
NSLog(@"DONE!"); 

// Finalize the output file 
CGPDFContextClose(writeContext); 

// Release from memory 
CFRelease(pdfURL1); 
CFRelease(pdfURL2); 
CFRelease(pdfURLOutput); 
CGPDFDocumentRelease(pdfRef1); 
CGPDFDocumentRelease(pdfRef2); 
CGContextRelease(writeContext); 
+0

你可以檢查這個SO鏈接http://stackoverflow.com/questions/16646039/how-to-save-created-pdf-in-document-folder-and-merge-in-ios我正在運行相同的問題 –

+0

我完成了你的上面的代碼:) 1up的答案... –

-1
$mergedPath = '/tmp/bulkinvoice_merge_'.$vendor_id.'_'.$randomno.'.pdf'; 
$filepath = 'http://xyz/'.$vendor_id.'/'.$shipmentId.'_invoice_shipment_label.pdf'; 
$current = file_get_contents($filepath); 
$tempPath = '/tmp/bulkinvoice_'.$vendor_id.'_'.$shipmentId.'.pdf'; 
chmod($tempPath, 777); 
file_put_contents($tempPath,$current); 
$pdf = Zend_Pdf::load($tempPath); 

foreach($pdf->pages as $page){ 
    $pdfExtract = $ex->clonePage($page); 
    $pdf2show->pages[] = $pdfExtract; 
} 

//$pdf2show->pages[] = $ex->clonePage($pdf->pages[0]); 

echo "\n [->]Added in merged PDF : ".$shipmentId; 
$pdf2show->save($mergedPath); 
unlink($tempPath); 
相關問題