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

  • Home
  • SEARCH
  • 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 7022929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:38:44+00:00 2026-05-27T23:38:44+00:00

How would you save an NSImage to a PDF file with Foundation? This is

  • 0

How would you save an NSImage to a PDF file with Foundation? This is not a GUI app so AppKit (and therefore NSView) is not in use.

EDIT: Well, I feel silly now. NSImage is part of AppKit, so it is in use. However, my question still stands: how do you save the NSImage to a PDF?

  • 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-05-27T23:38:45+00:00Added an answer on May 27, 2026 at 11:38 pm

    By making a connection to the window server you can use NSImage and NSView. You can make that connection to the window server by using the AppKit function NSApplicationLoad.

    main.m

    #include <AppKit/AppKit.h>
    
    int main(int argc, const char **argv) {
        if(argc != 3) {
            fprintf(stderr, "Usage: %s source_img dest_pdf\n", argv[0]);
            exit(1);
        }
    
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
        BOOL success;
        NSString *imgPath, *pdfPath;
        NSImage *myImage;
        NSImageView *myView;
        NSRect vFrame;
        NSData *pdfData;
    
        imgPath = [NSString stringWithUTF8String:argv[1]];
        pdfPath = [NSString stringWithUTF8String:argv[2]];
    
        /* Calling NSApplicationLoad will give a Carbon application a connection
        to the window server and enable the use of NSImage, NSView, etc. */
        success = NSApplicationLoad();
        if(!success) {
            fprintf(stderr, "Failed to make a connection to the window server\n");
            exit(1);
        }
    
        /* Create image */
        myImage = [[NSImage alloc] initWithContentsOfFile:imgPath];
        if(!myImage) {
            fprintf(stderr, "Failed to create image from path %s\n", argv[1]);
            exit(1);
        }
    
        /* Create view with that size */
        vFrame = NSZeroRect;
        vFrame.size = [myImage size];
        myView = [[NSImageView alloc] initWithFrame:vFrame];
    
        [myView setImage:myImage];
        [myImage release];
    
        /* Generate data */
        pdfData = [myView dataWithPDFInsideRect:vFrame];
        [pdfData retain];
        [myView release];
    
        /* Write data to file */
        success = [pdfData writeToFile:pdfPath options:0 error:NULL];
        [pdfData release];
        if(!success) {
            fprintf(stderr, "Failed to write PDF data to path %s\n", argv[2]);
            exit(1);
        }
    
        [pool release];
        return 0;
    }
    

    Compile this with the frameworks Foundation and AppKit linked:

    gcc -framework Foundation -framework AppKit main.m
    

    When you compiled it you can use it like this:

    ./a.out myImage.png outFile.pdf
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This would save enormous amounts of time, as Firebug is what I use to
So.. I have a buffer with MP3 data (If I would save this buffer
I am attempting to save an rtf file using FileDialog and would like to
I would like to save and read an ICO file. It doesnt matter if
How do I copy a buffer that would save to a ".BMP" file to
I'm creating this simple program which would save me alot of time, but I'm
I have an app that save the user image but i would save with
After getting an answer from this thread, it would save me alot of retyping
This is a long shot but if I could do this it would save
I would like to save the programs settings every time the user exits the

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.