Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

PDF Creation Tutorial

danielb21danielb21 Posts: 48Tutorial Authors, Registered Users
edited March 2012 in iPhone SDK Tutorials
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.
Post edited by danielb21 on
«13456

Replies

  • piratemc74piratemc74 Posts: 12Registered Users
    edited February 2009
    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<br />
    24" iMac<br />
    8gb iPhone-Gen1
  • AsaAsa Posts: 45Registered Users
    edited February 2009
    I am very interested to see this all the way...Please make part II and if possible a video :)

    Thanks!
  • meowmix23Fmeowmix23F Posts: 144Tutorial Authors
    edited February 2009
    piratemc74 wrote: »
    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.
  • TheFuryTheFury Posts: 40Registered Users
    edited February 2009
    meowmix23F wrote: »
    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.
  • meowmix23Fmeowmix23F Posts: 144Tutorial Authors
    edited February 2009
    TheFury wrote: »
    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.
  • TheFuryTheFury Posts: 40Registered Users
    edited February 2009
    meowmix23F wrote: »
    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/iphone-sdk-development/9012-sending-attachments-email-iphone-app.html
  • gliderglider Posts: 37Registered Users
    edited February 2009
    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 ?
    Moving map sectionals for the iPhone <a href="http://www.skycharts.net"; target="_blank">http://www.skycharts.net</a>;
  • tkilmertkilmer Posts: 583Registered Users
    edited February 2009
    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.
  • gliderglider Posts: 37Registered Users
    edited February 2009
    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).
    Moving map sectionals for the iPhone <a href="http://www.skycharts.net"; target="_blank">http://www.skycharts.net</a>;
  • CoasterCoaster Posts: 57Registered Users
    edited March 2009
    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
  • ledavid71ledavid71 Posts: 7New Users
    edited March 2009
    Hi I would like to know how you can display the pdf file on a view Im kind of lost.

    Thanks
  • danielb21danielb21 Posts: 48Tutorial Authors, Registered Users
    edited March 2009
    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
  • ThirtyOneThirtyOne Posts: 149Registered Users
    edited March 2009
    This is a great example! Thanks for the help.
  • sunny deesunny dee Posts: 106Registered Users
    edited May 2009
    I have a long string that I want to add to the pdf.

    Any ideas on how to wrap the text? Thanks.

    Sunny
  • danielb21danielb21 Posts: 48Tutorial Authors, Registered Users
    edited May 2009
    sunny dee wrote: »
    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. :)
  • prameshuprameshu Posts: 10Registered Users
    edited May 2009
    danielb21 wrote: »
    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.
  • prameshuprameshu Posts: 10Registered Users
    edited May 2009
    Hi Sunny,
    If you have this solution, could you please share it with me...
    thanks,
    Ramesh.
    sunny dee wrote: »
    I have a long string that I want to add to the pdf.

    Any ideas on how to wrap the text? Thanks.

    Sunny
  • danielb21danielb21 Posts: 48Tutorial Authors, Registered Users
    edited May 2009
    Refer to post #25 for this.
  • prameshuprameshu Posts: 10Registered Users
    edited May 2009
    thanks Daniel.
  • fishfish Posts: 183Registered Users
    edited May 2009
    Cheers, very help tutorial.
  • chaseactonchaseacton United StatesPosts: 550New Users @ @ @
    edited June 2009
    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
  • decaydecay Posts: 10Registered Users
    edited June 2009
    danielb21 wrote: »
    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.
  • danielb21danielb21 Posts: 48Tutorial Authors, Registered Users
    edited June 2009
    chaseacton wrote: »
    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
  • danielb21danielb21 Posts: 48Tutorial Authors, Registered Users
    edited June 2009
    decay wrote: »
    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.
  • danielb21danielb21 Posts: 48Tutorial Authors, Registered Users
    edited June 2009
    Spent more time today exploring the [NSString drawInRect:] method to see if I could get it to work, and I now have a working solution. The key is that you have to push the graphics context onto the stack. Below is how it works:
    NSString *string = @"Text to print.";
    
    UIGraphicsPushContext(context);
    CGContextTranslateCTM(context, 0, 20);
    CGContextScaleCTM(context, 1.0, -1.0);
    [string drawInRect:CGRectMake(0, 0, 300, 200) withFont:[UIFont fontWithName:@"Verdana" size:9]];
    UIGraphicsPopContext();
    

    So, the above code block pushes the context onto the stack, reverses the coordinates so that the text is drawn correctly, and then uses the drawInRect: withFont: method to draw the text, and finally calls the UIGraphicsPopContext() method.

    It's important to note that each time you call UIGraphicsPushContext(context); you must also call the pop context method, otherwise you'll be left with nothing.

    Thanks to decay for pushing me to investigate this more and enjoy everyone!
«13456
Sign In or Register to comment.