telliott99
telliott99

Reputation: 7907

Command line app to draw pdf in OS X

I am trying to figure out a good way to make a command line app (OS X on the Desktop) that draws and saves a pdf. If I had a GUI with a view, then I would use NSBezierPath objects and at the end do [myView dataWithPDFInsideRect:r].

I've been using a CGContextRef obtained with CGPDFContextCreateWithURL but it's clunky. Something simple like CGContextAddCurveToPoint with two control points doesn't render as I'd expect (it looks weird in the middle). And sophisticated things like Scott Stevenson's (part II) don't seem possible.

Am I missing something simple? Is there a way to construct a view and draw to it without a window? A way to do fancier core graphics?

UPDATE: Just to be clear, the code in main that works is:

int main(int argc, char * argv[]) {
    MyView *myView;
    NSRect f = NSMakeRect(0,0,300,300);
    myView = [[MyView alloc] initWithFrame:f];
    NSData *data = [myView dataWithPDFInsideRect:f];
    [data writeToFile:@"x.pdf" atomically:YES];
    return 0;
}

From Terminal:

clang prog.m -o prog -framework Cocoa -fobjc-gc-only
./prog

Upvotes: 0

Views: 359

Answers (1)

Jon Hess
Jon Hess

Reputation: 14247

If you make a command line tool that links AppKit and uses dataWithPDFInsideRect on a standalone view with no window, and run it manually from terminal.app, this will work like you expect. If you're logged in via ssh, or something else like a launchd job, this may not work.

Upvotes: 2

Related Questions