• 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
Creating a PDF file from NSString without showing it onscreen
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Creating a PDF file from NSString without showing it onscreen


  • Subject: Creating a PDF file from NSString without showing it onscreen
  • From: Paul Archibald <email@hidden>
  • Date: Tue, 26 Aug 2008 13:03:08 -0700

Comrades -

A while back I posted a question about creating a PDF file. I got a good, simple answer from Chuck which has worked in one test I have tried, but which I want to generalize. The first test used a custom view in the nib file, which I inserted text into, and then wrote out to the file. However, what I really would like is to be able to send an NSString (in)directly to the PDF file.

I am using the PDFImageView form Apple's PDFView sample project.

Here is my snippet:

- (void) renderToPDFFile: (NSString*) text :(NSString*) file {
	// PDFImageView isa NSImageView
	PDFImageView* pdfview = [[PDFImageView alloc] init];
	[pdfview setHidden:YES];
	[pdfview insertText:text];

	NSRect pdfbounds = [pdfview bounds];
	NSTextView* tview = [[NSTextView alloc] initWithFrame:pdfbounds];
	[tview insertText: text];		

	NSRect r = [tview bounds];
	NSData *data = [tview dataWithPDFInsideRect:r];
	[data writeToFile:file atomically:YES];
}

I do get a pdf file out of this, but it has no content. Debugging it, I see that my pdfbounds rectangle is not properly sized, but is just {0,0,0,0}.

I suspect that the problem I am having now is that I am not properly initializing the PDFImageView. (In my first test I had a normal nib with a PDFImageView displayed in the main window). In the "real" program I don't have a spare window to render the text into, so I would like to do all the work without any visible elements.

So, how do I create a view (PDFImageView or NSImageView) without having it in the nib? Do I need a window to put the view in? If so, does that window need to be visible?

As usual, if someone can just point me in the right direction, that would be swell.

Paul


On Aug 6, 2008, at 12:03 PM, email@hidden wrote:


It looks like PDFKit is aimed at simply reading and navigating within
an existing PDF file, but what I need to do is create a new
PDF and   write some simple text to it. I know that the Preview app
does that  for a lot of apps, but I would like to bypass that, too. I
simply want my app to create a PDF file, open it for writing,
write some  simple text to it, and save it to disk.

Easiest way: Create a view that draws what you want, send it - dataWithPDFInsideRect: and write that data to a file.


Cheers,
Chuck

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Creating a PDF file from NSString without showing it onscreen
      • From: Andy Lee <email@hidden>
  • Prev by Date: Re: Semi-transparent, blurred NSWindow background?
  • Next by Date: Re: Creating a PDF file from NSString without showing it onscreen
  • Previous by thread: Re: [ANNOUNCE] SQLite Persisted Objects for Cocoa
  • Next by thread: Re: Creating a PDF file from NSString without showing it onscreen
  • Index(es):
    • Date
    • Thread