Jan25
Joining many jpeg files into one single PDF
I had old scans of many guitar tab books as a directory of JPEG files, sequentially numbered. That was the way the old scanner stored documents. My SnapScan has an automatic document feeder and scans directly to PDF, which is much nicer to use. I wanted to join these JPEGs into a single PDF. After looking for different ways to do it, AppleScript, available software, etc., decided none of them were acceptable for my needs.
To accomplish the task, I used the following process (not optimal, but works):
cd directory
ls *.jpg | sort | xargs SavePDF
Where SavePDF is a program I hacked in an hour or so, using the CoreImage interfaces on the Mac. The code for SavePDF is shown below. Compile it with XCode, as of this writing Version 2.4.1
Precompiled binary for Mac OS X Intel platform is available here (in some browsers you’ll need to right-click and select “Save As…”)
Note that there is no error checking at all for the arguments. The resulting file is left at the current directory with the hardwired name “joined.pdf”
Use at your own risk - Works for me, YMMV. Other formats besides JPEG are supported by CoreImage and this code will work also with BMPs, PNGs and others. I personally have not used it for any other format.
Listing of SavePDF.m
/**********************************************************************************************/
/* Copyright (c) 2007 Juan C. Mendez (jcmendez@alum.mit.edu) */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining a copy of this */
/* software and associated documentation files (the "Software"), to deal in the Software */
/* without restriction, including without limitation the rights to use, copy, modify, merge, */
/* publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons */
/* to whom the Software is furnished to do so, subject to the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be included in all copies or */
/* substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, */
/* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR */
/* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE */
/* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR */
/* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER */
/* DEALINGS IN THE SOFTWARE. */
/**********************************************************************************************/
#import
#include
#define kFileCreator 'prvw'
#define kFileTypePDF 'PDF '
OSErr CreatePDFFile (int argc, const char *argv[], const char *outFile) {
OSErr err = 0;
CGContextRef pdfContext;
CFStringRef path;
CFURLRef origUrl,destUrl;
CGImageSourceRef imageSource;
CGImageRef image;
if (outFile == NULL) return -1;
// Let's see if the destination URL can be created
@try {
path = CFStringCreateWithCString (NULL, outFile, kCFStringEncodingUTF8);
destUrl = CFURLCreateWithFileSystemPath (NULL, path, kCFURLPOSIXPathStyle, 0);
}
@catch (NSException *e) {
NSLog(@"CreatePDFFile setting target: Caught %@: %@", [e name], [e reason]);
err = -1;
}
@finally {
if (path) CFRelease (path);
}
@try {
int i;
pdfContext = CGPDFContextCreateWithURL (destUrl, NULL, NULL);
for (i=1; i
Juan C. Mendez Says:
I found a utility that can be freely downloaded that also allows joining image files into PDFs. Advantages: nicer user interface (not a command line utility), allows reordering files on a window. Disadvantages: Not as easy to integrate into an automated workflow as my program. Here is the link: http://www.monkeybreadsoftware.info/Freeware/CombinePDFs.shtml
ard Says:
If you don’t want to go into compliing etc. install cups-pdf , drag and drop the jpgs on the printer icon, download , drag and drop the generated pdfs onto pdflab, save the file, presto. With thanks to all the wonderful mac programmers out there!