1
/* pdf2jpeg -- converts a PDF file to a JPEG file, using Cocoa
3
* Copyright (c) 2003 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;
29
for (i = 1; i < argc; i++)
32
if (argv[i][0] == '-' && argv[i][1] == '-')
34
if (!strcmp (argv[i], "-q") ||
35
!strcmp (argv[i], "-qual") ||
36
!strcmp (argv[i], "-quality"))
39
if (1 != sscanf (argv[++i], " %d %c", &q, &c) ||
42
fprintf (stderr, "%s: quality must be 5 - 100 (%d)\n",
46
compression = q / 100.0;
48
else if (!strcmp (argv[i], "-verbose"))
50
else if (!strcmp (argv[i], "-v") ||
51
!strcmp (argv[i], "-vv") ||
52
!strcmp (argv[i], "-vvv"))
53
verbose += strlen(argv[i])-1;
54
else if (argv[i][0] == '-')
56
fprintf (stderr, "%s: unknown option %s\n", progname, argv[i]);
67
"usage: %s [-verbose] [-quality NN] "
68
"infile.pdf outfile.jpg\n",
74
if (!infile || !outfile)
78
// Much of Cocoa needs one of these to be available.
79
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
81
//Need an NSApp instance to make [NSImage TIFFRepresentation] work
82
NSApp = [NSApplication sharedApplication];
86
fprintf (stderr, "%s: reading %s...\n", progname, infile);
88
// Load the PDF file into an NSData object:
89
NSData *pdf_data = [NSData dataWithContentsOfFile:
90
[NSString stringWithCString:infile]];
92
// Create an NSPDFImageRep from the data:
93
NSPDFImageRep *pdf_rep = [NSPDFImageRep imageRepWithData:pdf_data];
95
// Create an NSImage instance
96
NSImage *image = [[NSImage alloc] initWithSize:[pdf_rep size]];
98
// Draw the PDFImageRep in the NSImage
100
[pdf_rep drawAtPoint:NSMakePoint(0.0,0.0)];
103
// Load the NSImage's contents into an NSBitmapImageRep:
104
NSBitmapImageRep *bit_rep = [NSBitmapImageRep
105
imageRepWithData:[image TIFFRepresentation]];
107
// Write the bitmapImageRep to a JPEG file:
110
fprintf (stderr, "%s: error converting image?\n", argv[0]);
115
fprintf (stderr, "%s: writing %s (%d%% quality)...\n",
116
progname, outfile, (int) (compression * 100));
118
NSDictionary *props = [NSDictionary
119
dictionaryWithObject:
120
[NSNumber numberWithFloat:compression]
121
forKey:NSImageCompressionFactor];
122
NSData *jpeg_data = [bit_rep representationUsingType:NSJPEGFileType
125
[jpeg_data writeToFile:
126
[NSString stringWithCString:outfile]