Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7907405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:24:56+00:00 2026-06-03T11:24:56+00:00

I have a graphics-related Document-based Mac App. My app’s Documents may have multiple pages.

  • 0

I have a graphics-related Document-based Mac App. My app’s Documents may have multiple “pages”. Each page has an NSView “canvas” object.

My app has several export options which are implemented as methods which return an NSData object (which is then written to disk).

I would like to implement a PDF export option in a method which:

  1. Creates an in-memory PDF
  2. loops thru my document’s canvas views and renders each in a new page in the in-memory PDF
  3. returns the in-memory, multi-page PDF as an NSData

The code below is what I am currently trying.

Each page in my document is 800 x 600 pixels.

When I write the resulting NSData object to disk, the write operation succeeds, but the file on disk is corrupted somehow. The file cannot be opened in Preview or any other app I’ve tried.

What am I doing wrong?

NSMutableData *data = [NSMutableData data];
CGDataConsumerRef consumer = CGDataConsumerCreateWithCFData((CFMutableDataRef)data);

CGRect mediaBox = CGRectMake(0.0, 0.0, 800.0, 600.0);
CGContextRef ctx = CGPDFContextCreate(consumer, &mediaBox, NULL);
CFRelease(consumer);

NSGraphicsContext *gc = [NSGraphicsContext graphicsContextWithGraphicsPort:ctx flipped:NO];

for (NSView *canvas in myCanvases) {
    CGContextBeginPage(ctx, &mediaBox);
    CGContextSaveGState(ctx);

    [canvas displayRectIgnoringOpacity:mediaBox inContext:gc];

    CGContextRestoreGState(ctx);
    CGContextEndPage(ctx);
}

CGPDFContextClose(ctx); // UPDATE: this line was originally missing. see answer below
CGContextRelease(ctx);

...

NSError *err = nil;
if (![data writeToFile:s options:NSDataWritingAtomic error:&err]) {
    if (err) {
        NSLog(@"%@", err);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T11:25:02+00:00Added an answer on June 3, 2026 at 11:25 am

    OP here. I have solved the problem. I was missing a final call to CGPDFContextClose().

    So before releasing the context…

    CGPDFContextClose(ctx);
    CGContextRelease(ctx);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a graphics editing cocoa app on Mac OSX that produces 32 by
I have a MFC document/view C++ graphics application that does all its drawing to
We have a web service iPhone app which fetches short texts (no graphics) in
I have a Graphics object of JPanel and that is working fine: import java.awt.Color;
I have done a graphics project using OpenGL and now I want to convert
I have an android.graphics.bitmap and an android.net.Uri both of these I can use anyway
So I am web developer who used to have a kind graphics design person
In my code, let's say I have the PaintObject(Graphics g) . In some other
I have a program that draws some vector graphics using System.Drawing and the Graphics
I have a system with an NVidia graphics card and I'm looking at using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.