• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How can I create a PDF document from a string in a non-GUI application?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How can I create a PDF document from a string in a non-GUI application?


  • Subject: Re: How can I create a PDF document from a string in a non-GUI application?
  • From: Scott Thompson <email@hidden>
  • Date: Wed, 17 May 2006 13:13:47 -0500


On May 17, 2006, at 7:49 AM, email@hidden wrote:

Hi all.

I am trying to create and save a PDF document where the contents of the PDF file is derived from a passed-in NSString. The method in question is in an application without a graphical component, so a solution using NSView's dataWithPDFInsideRect: seem to be out, as does using NSPrintOperation and its printOperationWithView:printInfo: method as this also takes an NSView as an argument.

I have also looked at PDFKit's PDFDocument intWithData:, but as I only have a string and this method requires PDF data encapsulated in an NSData object, this isn't helping much (passing in the string contents as a NSData object drew a blank)!

Does anyone have any ideas as to where I can direct my efforts, or have I overlooked something in the methods I mentioned earlier?

You can use Core Graphics to do this directly. The general technique would be to take the NSString, build a Cocoa text system from it directly:


<http://developer.apple.com/documentation/Cocoa/Conceptual/ TextArchitecture/Tasks/AssembleSysByHand.html>

You'll need an NSLayoutManager, NSContainer, and NSTextStorage but you don't need the text view.

Then you can create a Core Graphics PDF context

<http://developer.apple.com/documentation/GraphicsImaging/Conceptual/ drawingwithquartz2d/dq_context/chapter_3_section_3.html>

You can create an NSGraphicsContext on the CGContext using

+ (NSGraphicsContext *)graphicsContextWithGraphicsPort:(void *) graphicsPort flipped:(BOOL)initialFlippedState;

and set it to be the current graphics context then you can call the NSLayoutManager's routines:

–  drawBackgroundForGlyphRange:atPoint:
–  drawGlyphsForGlyphRange:atPoint:

To actually draw the string on the PDF context.

There is one trick to all this that is particularly insidious. When you create the NSGraphicsContext, it will keep a reference to the PDF context through the autorelease pool. The PDF context MUST be destroyed before the PDF it creates will be written out fully. So the easiest way to handle it is to create teh NSGraphicsContext in an evnironment with your own autorelease pool so you can control it's lifetype more precisely.

Scott _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >How can I create a PDF document from a string in a non-GUI application? (From: email@hidden)

  • Prev by Date: Protected Audio/Video
  • Next by Date: Re: programmatically determining if the machine is intel or not
  • Previous by thread: How can I create a PDF document from a string in a non-GUI application?
  • Next by thread: Re: How can I create a PDF document from a string in a non-GUI application?
  • Index(es):
    • Date
    • Thread