[MacRuby-devel] Creating a pdf : Cocoa to MacRuby translation

Julien Jassaud sojastar07 at gmail.com
Thu Nov 4 03:12:51 PDT 2010


Hello to every one,

I am trying to programmatically create a pdf. I have this quite simple code in Cocoa that works perfectly :

#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {

  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

  CGContextRef pdfContext;
  CFStringRef path;
  CFURLRef url;
  CGRect pageRect;

  path = CFStringCreateWithCString(NULL, "./my_pdf.pdf", kCFStringEncodingUTF8);
  url = CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, 0);
  CFRelease (path);

  pageRect = NSMakeRect(0,0,100,100);

  pdfContext = CGPDFContextCreateWithURL(url, &pageRect, nil);
  CFRelease(url);

  CGPDFContextBeginPage(pdfContext, nil);

  CGContextSetCMYKFillColor(pdfContext, 1.0, 0.0, 0.0, 0.0, 1.0);
  CGContextFillRect(pdfContext, NSMakeRect(30, 30, 40, 40));

  CGPDFContextEndPage(pdfContext);

  CGPDFContextClose(pdfContext);

  [pool drain];
  return 0;

}


After running this program I get a pdf in perfect working order. I tried to port this little example to MacRuby like so :

framework 'Cocoa'

url = CFURLCreateWithFileSystemPath(nil, './my_pfd.pdf', KCFURLPOSIXPathStyle, 0)

page_rectangle         = NSMakeRect(0,0,100,100)
page_rectangle_pointer = Pointer.new_with_type('{CGRect={CGPoint=dd}{CGSize=dd}}')

pdf_context = CGPDFContextCreateWithURL(url, page_rectangle_pointer, nil)

CGPDFContextBeginPage(pdf_context, nil)

CGContextSetCMYKFillColor(pdf_context, 1.0, 0.0, 0.0, 0.0, 1.0)
CGContextFillRect(pdf_context, NSMakeRect(30, 30, 40, 40))

CGPDFContextEndPage(pdf_context)
CGPDFContextClose(pdf_context)

This do produces a  pdf but it is completely empty. Also, MacRuby complains when called with the -w option :

$ macruby -w cocoapdf.rb
unknown: warning: instance variable __octype__ not initialized


Does anybody have an idea what I am doing wrong ? 

Thanks,
Julien Jassaud

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-devel/attachments/20101104/4fa219fd/attachment-0001.html>


More information about the MacRuby-devel mailing list