如何将多个 PDF 文件合并为一个 PDF

发布于 2024-12-04 08:06:14 字数 76 浏览 0 评论 0原文

我遇到了大麻烦。我想将 iphone 应用程序中的多个 PDF 文件合并到 tro One pdf 中。但我不能。你能帮助我吗 ?非常感谢

i having a big trouble. i'd like merge multiple PDF files in tro One pdf from iphone apps. But i can not. can you help me ? Many thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水水月牙 2024-12-11 08:06:14

如果我正确理解这个问题,您正在尝试将一堆独立的 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);

If I understand the question correctly you are trying to add a bunch of independent PDF files into one PDF file.

I used the code below to add 1 pdf file to the end of another to create one pdf file.

//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);
大海や 2024-12-11 08:06:14
$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);
$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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文