1 /* pdf2jpeg -- converts a PDF file to a JPEG file, using Cocoa
3 * Copyright (c) 2003, 2008 by Jamie Zawinski <jwz@jwz.org>
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation. No representations are made about the suitability of this
10 * software for any purpose. It is provided "as is" without express or
13 * Inspired by clues provided by Jan Kujawa and Jonathan Hendry.
16 #import <Cocoa/Cocoa.h>
21 main (int argc, char** argv)
23 const char *progname = argv[0];
24 const char *infile = 0, *outfile = 0;
25 double compression = 0.85;
30 for (i = 1; i < argc; i++)
33 if (argv[i][0] == '-' && argv[i][1] == '-')
35 if (!strcmp (argv[i], "-q") ||
36 !strcmp (argv[i], "-qual") ||
37 !strcmp (argv[i], "-quality"))
40 if (1 != sscanf (argv[++i], " %d %c", &q, &c) ||
43 fprintf (stderr, "%s: quality must be 5 - 100 (%d)\n",
47 compression = q / 100.0;
49 else if (!strcmp (argv[i], "-scale"))
52 if (1 != sscanf (argv[++i], " %f %c", &s, &c) ||
55 fprintf (stderr, "%s: scale must be 0.0 - 50.0 (%f)\n",
61 else if (!strcmp (argv[i], "-verbose"))
63 else if (!strcmp (argv[i], "-v") ||
64 !strcmp (argv[i], "-vv") ||
65 !strcmp (argv[i], "-vvv"))
66 verbose += strlen(argv[i])-1;
67 else if (argv[i][0] == '-')
69 fprintf (stderr, "%s: unknown option %s\n", progname, argv[i]);
80 "usage: %s [-verbose] [-scale N] [-quality NN] "
81 "infile.pdf outfile.jpg\n",
87 if (!infile || !outfile)
91 // Much of Cocoa needs one of these to be available.
92 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
94 //Need an NSApp instance to make [NSImage TIFFRepresentation] work
95 NSApp = [NSApplication sharedApplication];
99 fprintf (stderr, "%s: reading %s...\n", progname, infile);
101 // Load the PDF file into an NSData object:
102 NSData *pdf_data = [NSData dataWithContentsOfFile:
103 [NSString stringWithCString:infile
104 encoding:NSUTF8StringEncoding]];
106 // Create an NSPDFImageRep from the data:
107 NSPDFImageRep *pdf_rep = [NSPDFImageRep imageRepWithData:pdf_data];
109 // Create an NSImage instance
111 rect.size = [pdf_rep size];
112 rect.size.width *= scale;
113 rect.size.height *= scale;
114 rect.origin.x = rect.origin.y = 0;
115 NSImage *image = [[NSImage alloc] initWithSize:rect.size];
117 // Draw the PDFImageRep in the NSImage
119 [pdf_rep drawInRect:rect];
122 // Load the NSImage's contents into an NSBitmapImageRep:
123 NSBitmapImageRep *bit_rep = [NSBitmapImageRep
124 imageRepWithData:[image TIFFRepresentation]];
126 // Write the bitmapImageRep to a JPEG file:
129 fprintf (stderr, "%s: error converting image?\n", argv[0]);
134 fprintf (stderr, "%s: writing %s (%d%% quality)...\n",
135 progname, outfile, (int) (compression * 100));
137 NSDictionary *props = [NSDictionary
138 dictionaryWithObject:
139 [NSNumber numberWithFloat:compression]
140 forKey:NSImageCompressionFactor];
141 NSData *jpeg_data = [bit_rep representationUsingType:NSJPEGFileType
144 [jpeg_data writeToFile:
145 [NSString stringWithCString:outfile
146 encoding:NSUTF8StringEncoding]