Creating a PDF file from NSString without showing it onscreen
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