Merging PDF pages

Chris Heidecker hechris at ziggo.nl
Mon Aug 28 06:02:11 EDT 2023


Hi,

While applescript is still around, you could use this script. Works pretty well.

https://www.macscripter.net/t/applescript-objective-c-handler-to-merge-pdf-files/72534

Regards,
Chis Heidecker


use scripting additions
use framework "Foundation"
use framework "Quartz" -- required for PDF stuff

set inFiles to (choose file of type {"pdf"} with prompt "Choose your PDF files:" with multiple selections allowed)
set destPosixPath to POSIX path of (choose file name default name "Combined.pdf" with prompt "Save new PDF to:")
its combineFiles:inFiles savingTo:destPosixPath

on combineFiles:inFiles savingTo:destPosixPath
-- make URL of the first PDF
set inNSURL to current application's class "NSURL"'s fileURLWithPath:(POSIX path of item 1 of inFiles)
-- make PDF document from the URL
set theDoc to current application's PDFDocument's alloc()'s initWithURL:inNSURL
-- loop through the rest
set oldDocCount to theDoc's pageCount()
set inFiles to rest of inFiles
repeat with aFile in inFiles
-- make URL of the next PDF
set inNSURL to (current application's class "NSURL"'s fileURLWithPath:(POSIX path of aFile))
-- make PDF document from the URL
set newDoc to (current application's PDFDocument's alloc()'s initWithURL:inNSURL)
-- loop through, moving pages
set newDocCount to newDoc's pageCount()
repeat with i from 1 to newDocCount
-- get page of old PDF
set thePDFPage to (newDoc's pageAtIndex:(i - 1)) -- zero-based indexes
-- insert the page
(theDoc's insertPage:thePDFPage atIndex:oldDocCount)
set oldDocCount to oldDocCount + 1
end repeat
end repeat
set outNSURL to current application's class "NSURL"'s fileURLWithPath:destPosixPath
-- save the new PDF
(theDoc's writeToURL:outNSURL)
end combineFiles:savingTo:



> Op 25 aug. 2023, om 23:15 heeft Eller, Roger via use-livecode <use-livecode at lists.runrev.com> het volgende geschreven:
> 
> Thank you for this and all the details, Matthias.  I will give it a try.
> 
> 
> ~Roger
> 
> ________________________________
> From: use-livecode <use-livecode-bounces at lists.runrev.com> on behalf of matthias rebbe via use-livecode <use-livecode at lists.runrev.com>
> Sent: Friday, August 25, 2023 2:16 PM
> To: How to use LiveCode <use-livecode at lists.runrev.com>
> Cc: matthias_livecode_150811 at m-r-d.de <matthias_livecode_150811 at m-r-d.de>
> Subject: Re: Merging PDF pages
> 
> 
> CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.
> 
> 
> 
> Just noticed that my steps are missing something.
> So here's version 2 of it.
> 
> 1. download
> https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.pdflabs.com%2Ftools%2Fpdftk-the-pdf-toolkit%2Fpdftk_server-2.02-mac_osx-10.11-setup.pkg&data=05%7C01%7Croger.e.eller%40sealedair.com%7Ce1d5200433404eec54d008dba59780d1%7C2691a2514c384643af0b0c0982f197bd%7C0%7C0%7C638285842350859203%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=tEXnKnbF7xT3GjOMoYKIiM%2Fv3JwkS7pzKRM3op80KMI%3D&reserved=0<https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/pdftk_server-2.02-mac_osx-10.11-setup.pkg>
> 
> 2. to extract the pkg run the following in Terminal
> pkgutil --expand <source path of pkg>  <destinaltion path>
> 
> If the source path to the pkg contains spaces then put quotes around the path.
> 
> 3. Now open the destination path to which you've extracted the .pkg. There should be another pkg pdftk.pkg
> 
> 4. right click the file pdftk.pkg  and select "show package content"
> 
> 5. Now you should see a file payload. Right click that file and select "open with" and select the archive utility to decompress the file payload.
> 
> 6. After extraction you'll find a new folder "Payload 2"
> 
> 7. open that folder. In that folder you will find the Lib and Bin folder. Copy those folder to another location.
> 
> Now in LC you can use shell shell function to call the pdftk
> 
> Example
> set the folder to "path to the bin folder"
> 
> put shell("./pdftk  file1.pdf file2.pdf file3.pdf cat output mynewfile.pdf")
> 
> 
> would create combine the three file..pdf file into a new one called mynewfile.pdf
> 
> To use pdftk server in your standalone  you just have to include both folders in your standalone.
> 
> 
> On Windows you first have to run the setup of pdftk server. During the installation you can select the folder where pdftk server shall be installed. You then can copy the bin folder from that installation folder
> and use it with your standalone on any other computer without the need to run the setup on that computer.
> 
> 
> 
> 
>> Am 25.08.2023 um 17:00 schrieb matthias rebbe via use-livecode <use-livecode at lists.runrev.com>:
>> 
>> On which OS are you planning to do this?
>> 
>> On Windows and macOS you could make use of PDFTK server from PDFLabs.com <https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fpdflabs.com%2F&data=05%7C01%7Croger.e.eller%40sealedair.com%7Ce1d5200433404eec54d008dba59780d1%7C2691a2514c384643af0b0c0982f197bd%7C0%7C0%7C638285842350859203%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=2KHRMuPPbThuEdstyMStYxuVIocIbRsA9yaSB%2Fw%2F7dI%3D&reserved=0<http://pdflabs.com/>>
>> 
>> On macOS  PDFTK Server normally  has to be installed to use it. But you could follow these steps here to get it working w/o installation.
>> 
>> 1. Do not use the d/l link for pdftk Server from the pdfLabs website. That version will not work on macOS11 and up. Instead download this version of PDFTK Server. Sid Steward created a new version but did not update the d/l link
>> https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.pdflabs.com%2Ftools%2Fpdftk-the-pdf-toolkit%2Fpdftk_server-2.02-mac_osx-10.11-setup.pkg&data=05%7C01%7Croger.e.eller%40sealedair.com%7Ce1d5200433404eec54d008dba59780d1%7C2691a2514c384643af0b0c0982f197bd%7C0%7C0%7C638285842351015442%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=S5yjsFoOrBWQY%2FKUhQbD6WZxAzuRiBOK6Yrval2H1Q4%3D&reserved=0<https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/pdftk_server-2.02-mac_osx-10.11-setup.pkg>
>> 
>> 2. to extract the pkg run the following in Terminal
>> pkgutil --expand <source path of pkg>  <destinaltion path>
>> 
>> If the source path to the pkg contains spaces then put quotes around the path.
>> 
>> After extraction you'll find the 2 folders lib and bin and some others .The 2 mentioned ones are needed.
>> 
>> Now in LC you can use shell shell function to call the pdftk
>> 
>> Example
>> set the folder to "path to the bin folder"
>> 
>> put shell("./pdftk  file1.pdf file2.pdf file3.pdf cat output mynewfile.pdf")
>> 
>> 
>> would create combine the three file..pdf file into a new one called mynewfile.pdf
>> 
>> 
>> I wrote this in a hurry, so i might have some error in my explanation. Just ask, if you have problems.
>> 
>> Regards,
>> 
>> Matthias
>> 
>>> Am 25.08.2023 um 15:10 schrieb Eller, Roger via use-livecode <use-livecode at lists.runrev.com>:
>>> 
>>> It's been a while since I've posted a question here.  Is it possible to use livecode script to merge multiple 1-page PDFs into a multipage PDF without using Acrobat or other 3rd party software?
>>> 
>>> Thanks.
>>> 
>>> ~Roger
>>> 
>>> 
>>> _______________________________________________
>>> use-livecode mailing list
>>> use-livecode at lists.runrev.com
>>> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
>>> https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Flists.runrev.com%2Fmailman%2Flistinfo%2Fuse-livecode&data=05%7C01%7Croger.e.eller%40sealedair.com%7Ce1d5200433404eec54d008dba59780d1%7C2691a2514c384643af0b0c0982f197bd%7C0%7C0%7C638285842351015442%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=VUSlgWxqGFAlz72s3FBs5nbHZZnAoL54MZ4%2FU7ZYlXQ%3D&reserved=0<http://lists.runrev.com/mailman/listinfo/use-livecode>
>> 
>> _______________________________________________
>> use-livecode mailing list
>> use-livecode at lists.runrev.com
>> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
>> https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Flists.runrev.com%2Fmailman%2Flistinfo%2Fuse-livecode&data=05%7C01%7Croger.e.eller%40sealedair.com%7Ce1d5200433404eec54d008dba59780d1%7C2691a2514c384643af0b0c0982f197bd%7C0%7C0%7C638285842351015442%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=VUSlgWxqGFAlz72s3FBs5nbHZZnAoL54MZ4%2FU7ZYlXQ%3D&reserved=0<http://lists.runrev.com/mailman/listinfo/use-livecode>
> 
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Flists.runrev.com%2Fmailman%2Flistinfo%2Fuse-livecode&data=05%7C01%7Croger.e.eller%40sealedair.com%7Ce1d5200433404eec54d008dba59780d1%7C2691a2514c384643af0b0c0982f197bd%7C0%7C0%7C638285842351015442%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=VUSlgWxqGFAlz72s3FBs5nbHZZnAoL54MZ4%2FU7ZYlXQ%3D&reserved=0<http://lists.runrev.com/mailman/listinfo/use-livecode>
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode




More information about the use-livecode mailing list