Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Tutorials

Reply
 
LinkBack Thread Tools Display Modes
Old 02-10-2009, 03:56 PM   #1 (permalink)
Registered Member
 
Join Date: Sep 2008
Posts: 48
Default PDF Creation Tutorial

Good afternoon everyone!

I have written an intro to PDF tutorial for those that are interested in PDF interactivity within your iPhone apps. This first tutorial, shows how to simply create a PDF that contains an image, some text and a border around the page.

Ok, on to the goods! There are two custom methods in the PDFViewController.m file that you will be looking at mostly. The first is CreatePDFFile. This method does exactly what it says - creates a PDF file. The second method is called createPDF, and is an IBAction method hooked to an UIButton in IB. Upon touching the button this method calls our CreatePDFFile method.

Important - there is no output after clicking the button, meaning you will not see the resulting PDF displayed on the phone. I can create a second tutorial for that purpose, but it seemed like most people were only interested in the creation of the file.

In order to view the resulting PDF go to:
Users/YourUserName/Library/Application Support/iPhone Simulator/User/Applications

In this directory sort by Date Modified. If the example project here was the last app that you ran it will be at the top of this directory. Simply navigate to the documents directory of the PDF example app and you will be able to see the PDF it creates.

Let me know if you have any questions!

P.S. So it seems that the example project was larger than the allowed size for attachments. I will get the whole project up on the web for download soon, but in the meantime I have included the class files which is all you should need.
Attached Files
File Type: zip Classes.zip (4.4 KB, 5757 views)
danielb21 is offline   Reply With Quote
Old 02-11-2009, 04:16 PM   #2 (permalink)
Registered Member
 
Join Date: Feb 2009
Location: Cary, NC
Posts: 12
Send a message via AIM to piratemc74
Default Where's the tutorial?

Very interested in this. I know you attached the class files, but, "explanation" is good too :-)

Also, once the PDF is created, is there a way to then send it to the email tool? Does Apple allow that?

Thanks!
__________________
Mat
24" iMac
8gb iPhone-Gen1
piratemc74 is offline   Reply With Quote
Old 02-12-2009, 07:50 AM   #3 (permalink)
Asa
New Member
 
Join Date: Feb 2009
Location: Bangkok, Thailand
Posts: 45
Default Please Continue

I am very interested to see this all the way...Please make part II and if possible a video

Thanks!
Asa is offline   Reply With Quote
Old 02-21-2009, 12:39 PM   #4 (permalink)
Tutorial Author
 
Join Date: Jan 2009
Posts: 144
Default

Quote:
Originally Posted by piratemc74 View Post
Very interested in this. I know you attached the class files, but, "explanation" is good too :-)

Also, once the PDF is created, is there a way to then send it to the email tool? Does Apple allow that?

Thanks!
You'd have to use the mailto: url scheme to open up mail.app, and, on non-jailbroken phones it is impossible to use the attachment paramter of mailto:, so you'l have to write an SMTP client.
meowmix23F is offline   Reply With Quote
Old 02-21-2009, 02:10 PM   #5 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 40
Default

Quote:
Originally Posted by meowmix23F View Post
You'd have to use the mailto: url scheme to open up mail.app, and, on non-jailbroken phones it is impossible to use the attachment paramter of mailto:, so you'l have to write an SMTP client.
There is open source code that allows you to send an email with attachment. You can either have the user set up the parameters, or you can just set up a mail account that handles the outgoing mail.

I found the source somewhere on this messageboard. It is available via SVN. I tried it and it works well. I looked for the thread and can't seem to find it. Maybe someone else has the link.
TheFury is offline   Reply With Quote
Old 02-21-2009, 10:45 PM   #6 (permalink)
Tutorial Author
 
Join Date: Jan 2009
Posts: 144
Default

Quote:
Originally Posted by TheFury View Post
There is open source code that allows you to send an email with attachment. You can either have the user set up the parameters, or you can just set up a mail account that handles the outgoing mail.

I found the source somewhere on this messageboard. It is available via SVN. I tried it and it works well. I looked for the thread and can't seem to find it. Maybe someone else has the link.
Could you upload your copy of this code to sendspace or some other service?

I'd like to see this.
meowmix23F is offline   Reply With Quote
Old 02-22-2009, 01:31 AM   #7 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 40
Default

Quote:
Originally Posted by meowmix23F View Post
Could you upload your copy of this code to sendspace or some other service?

I'd like to see this.
Here is the thread I was referring:
http://www.iphonedevsdk.com/forum/ip...phone-app.html
TheFury is offline   Reply With Quote
Old 02-22-2009, 11:15 AM   #8 (permalink)
New Member
 
glider's Avatar
 
Join Date: Dec 2008
Posts: 37
Default

Thanks! But any progress on a viewer for multi page PDF's ?

Using CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, bounds, 0, true) - if you want to zoom in on large PDF it seems you are out of luck ?
glider is offline   Reply With Quote
Old 02-22-2009, 12:55 PM   #9 (permalink)
Registered Member
 
tkilmer's Avatar
 
Join Date: May 2008
Posts: 583
Default

Throw it in a webview. Not sure if that allows you to view multi-page pdfs but I know you can view PDFs in UIWebViews.
tkilmer is offline   Reply With Quote
Old 02-22-2009, 03:17 PM   #10 (permalink)
New Member
 
glider's Avatar
 
Join Date: Dec 2008
Posts: 37
Default

Thanks - yes, found that out. The UIWebView automatically handles multi page pdf's as well (besides zooming, and it looks GOOD unlike the lower CGPDF stuff).
glider is offline   Reply With Quote
Old 03-04-2009, 11:58 PM   #11 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 57
Default

This is great, thank you, any more information regarding this subject would be greatly appeciated.

I have nearly finished the main section of my app, and need to display about 25 values on a pdf, this pdf needs to be viewable though the App, but does not need to be enlarged.

I am hoping that then i can take a screenshot of the page, and print it via HB's Photo printer app. Would this be viable, or would some other format be better for this type of thing?


Thanks
Coaster is offline   Reply With Quote
Old 03-11-2009, 10:47 PM   #12 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 7
Default Display the PDF

Hi I would like to know how you can display the pdf file on a view Im kind of lost.

Thanks
ledavid71 is offline   Reply With Quote
Old 03-17-2009, 03:23 PM   #13 (permalink)
Registered Member
 
Join Date: Sep 2008
Posts: 48
Default

Hey everyone,

I've been extremely busy the past few weeks, so my apologies for being absent on this thread.

I will create another tutorial that covers viewing a PDF after creation, and will try to have it up either Saturday or Sunday of this week.

Dan
danielb21 is offline   Reply With Quote
Old 03-17-2009, 08:06 PM   #14 (permalink)
Registered Member
 
Join Date: Sep 2008
Posts: 149
Default

This is a great example! Thanks for the help.
ThirtyOne is offline   Reply With Quote
Old 05-22-2009, 06:48 PM   #15 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 106
Default

I have a long string that I want to add to the pdf.

Any ideas on how to wrap the text? Thanks.

Sunny
sunny dee is offline   Reply With Quote
Old 05-22-2009, 07:54 PM   #16 (permalink)
Registered Member
 
Join Date: Sep 2008
Posts: 48
Default

Quote:
Originally Posted by sunny dee View Post
I have a long string that I want to add to the pdf.

Any ideas on how to wrap the text? Thanks.

Sunny
The quickest way to do this is inside a for statement that takes a substring of your text for every x number of characters. This isn't a complete and robust text wrapping solution though. It's on my list of things to complete and I'll share my full solution when I'm done.
danielb21 is offline   Reply With Quote
Old 05-26-2009, 01:35 AM   #17 (permalink)
New Member
 
Join Date: May 2009
Posts: 10
Default new lines in the PDF file

Quote:
Originally Posted by danielb21 View Post
The quickest way to do this is inside a for statement that takes a substring of your text for every x number of characters. This isn't a complete and robust text wrapping solution though. It's on my list of things to complete and I'll share my full solution when I'm done.
Hi,
I have around 30 fields to display in the PDF file one by one. So could you please tell me how I can insert a newline character in the content...

thanks,
Ramesh.
prameshu is offline   Reply With Quote
Old 05-28-2009, 07:11 AM   #18 (permalink)
New Member
 
Join Date: May 2009
Posts: 10
Default insert newline characters in PDF

Hi Sunny,
If you have this solution, could you please share it with me...
thanks,
Ramesh.

Quote:
Originally Posted by sunny dee View Post
I have a long string that I want to add to the pdf.

Any ideas on how to wrap the text? Thanks.

Sunny
prameshu is offline   Reply With Quote
Old 05-28-2009, 11:35 AM   #19 (permalink)
Registered Member
 
Join Date: Sep 2008
Posts: 48
Default Text Wrapping...

Refer to post #25 for this.

Last edited by danielb21; 06-23-2009 at 12:42 PM.
danielb21 is offline   Reply With Quote
Old 05-29-2009, 01:01 AM   #20 (permalink)
New Member
 
Join Date: May 2009
Posts: 10
Default

thanks Daniel.
prameshu is offline   Reply With Quote
Old 05-29-2009, 06:22 AM   #21 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 184
Default

Cheers, very help tutorial.
fish is offline   Reply With Quote
Old 06-22-2009, 04:19 PM   #22 (permalink)
iOS Developer
 
chaseacton's Avatar
 
Join Date: Feb 2009
Location: United States
Posts: 532
Send a message via AIM to chaseacton Send a message via Skype™ to chaseacton
Default

Hey, if anyone knows how to view a locally stored pdf in a webview, please contact me. Im thankful fore any help and a xcode project would be very appreciated.

chaseacton@gmail.com
chaseacton is offline   Reply With Quote
Old 06-22-2009, 05:04 PM   #23 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 10
Default

Quote:
Originally Posted by danielb21 View Post
Ok, here is the text wrapping solution that I am using. This is very basic and ensures that the wrap happens at a "space".



If you have any questions, feel free to ask.
Actually, it does not have to be that complicated. You can use:

[NSString drawInRect:]

to get what you want. Just use the PDF context you are drawing to, and you have to perform a coordinate conversion, but if you make the rect high enough (you can calc this using [NSString sizeWithFont], all wrapping is done for you.
decay is offline   Reply With Quote
Old 06-22-2009, 09:23 PM   #24 (permalink)
Registered Member
 
Join Date: Sep 2008
Posts: 48
Default

Quote:
Originally Posted by chaseacton View Post
Hey, if anyone knows how to view a locally stored pdf in a webview, please contact me. Im thankful fore any help and a xcode project would be very appreciated.

chaseacton@gmail.com
Hey Chase,

There is another tutorial that I wrote that shows exactly how to do this. You'll find it a little further down in the tutorials section.

Thanks,
Dan
danielb21 is offline   Reply With Quote
Old 06-23-2009, 11:30 AM   #25 (permalink)
Registered Member
 
Join Date: Sep 2008
Posts: 48
Default

Quote:
Originally Posted by decay View Post
Actually, it does not have to be that complicated. You can use:

[NSString drawInRect:]

to get what you want. Just use the PDF context you are drawing to, and you have to perform a coordinate conversion, but if you make the rect high enough (you can calc this using [NSString sizeWithFont], all wrapping is done for you.
Hi decay,

Have you actually done what you are suggesting successfully or just basing your response on documentation provided by Apple? If you have, it would be greatly appreciated if you post your solution with a working example here and I will add it as a part of the tutorial with appropriate credit to you.

My experience with [NSString drawInRect:] in a PDFContext has been that it will not draw, citing an "invalid context". Had that not been the case I would have been using that all along.

Thanks,
Dan

*EDIT* Figured out how to use this, refer to post #25.

Last edited by danielb21; 06-23-2009 at 12:43 PM.
danielb21 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 245
18 members and 227 guests
ADY, Alsahir, beleg_1998, Dani77, e2applets, iDifferent, iph_s, JamesCahall, JasonR, mer10, Monstertaco, prchn4christ, Promo Dispenser, Robiwan, Rudy, smithdale87, spiderguy84, timle8n1
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,880
Threads: 89,228
Posts: 380,756
Top Poster: BrianSlick (7,129)
Welcome to our newest member, @sandris
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:17 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0