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

Matt Aimonetti mattaimonetti at gmail.com
Thu Nov 4 09:05:45 PDT 2010


Salut Julien,

I'm not really sure what's wrong with the pointer you are passing, but here
is how I got your code sample to work:

framework 'Cocoa'

path = CFStringCreateWithCString(nil, "./my_pdf.pdf", KCFStringEncodingUTF8)
url = CFURLCreateWithFileSystemPath(nil, path, KCFURLPOSIXPathStyle, 0)
page_rectangle = NSMakeRect(0,0,100,100)
pdf_context = CGPDFContextCreateWithURL(url, page_rectangle, 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)


- Matt

On Thu, Nov 4, 2010 at 3:12 AM, Julien Jassaud <sojastar07 at gmail.com> wrote:

> 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
>
>
> _______________________________________________
> MacRuby-devel mailing list
> MacRuby-devel at lists.macosforge.org
> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-devel/attachments/20101104/a4d48c1c/attachment.html>


More information about the MacRuby-devel mailing list