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:
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!
First of all thanks for the tutorial and the code above.
Can anyone tell me how to position a rotated text correctly?
I try this, but it's not correct:
i try to draw the content from scrollview to pdf but they draw only the visible content (that that i can see on the screen)not all the content on the scrollview
I can show the pdf file in web view from remote server...but i have to set an action to download the pdf to mobile...In PHP,i tried with force download which is working fine in desktop but not in iPhone...Instead it opens the pdf in webview....
Can anyone plz help me...?
Anyone else getting these errors when writing the text to the PDF?
Code:
: CGFont/Freetype: The function `get_subset_format' is currently unimplemented.
: can't get CIDs for glyphs for `Helvetica'.
I've found a thread on some other boards about this, and it sounds somewhat hopeless. The PDFs do view fine in most places, but for example the Google Docs viewer does not display the text. That, and you get a ton of errors in the console if you are writing a lot of text.
And to the couple of people having the EXC_BAD_ACCESS message, you need to add a Picture.png to your resources when you build this example.
i try to draw the content from scrollview to pdf but they draw only the visible content (that that i can see on the screen)not all the content on the scrollview
You are capturing the visible content only because your scroll view is sized match the visible content only, which it should be. You need to resize your scrollview to match the content you need to capture before you start the drawing code using something like this:
How will i send email in pdf format with contents taking from database?
Hi i just registered in the blog to download the helpful PDF creation codes from danielb21. Thanks dan for the helpful job. Can anyone help me in another part.
How will i send email in pdf format with contents taking from database?
I'm trying to implement this code in my app but CreatePDFFile crashes when code riches this line: provider = CGDataProviderCreateWithURL (pictureURL) EXC_BAD_ACCESS...
I don't know whats happening.
Where the code search for the png image than wants to include to the pdf flie?
The const char *picture was the name of the file to be included in the pdf?
EXC_BAD_ACCESS most often means you are trying to access memory which has already been released. Unfortunately the mistake in your code may not be anywhere near where the crash is actually occuring.
Try searching this forum and/or stackoverflow.com for EXC_BAD_ACCESS, you should find plenty of info on how to approach debugging this.
Quote:
Originally Posted by Balillas
Hi
I need some help.
I'm trying to implement this code in my app but CreatePDFFile crashes when code riches this line: provider = CGDataProviderCreateWithURL (pictureURL) EXC_BAD_ACCESS...
I don't know whats happening.
Where the code search for the png image than wants to include to the pdf flie?
The const char *picture was the name of the file to be included in the pdf?
EXC_BAD_ACCESS most often means you are trying to access memory which has already been released. Unfortunately the mistake in your code may not be anywhere near where the crash is actually occuring.
Try searching this forum and/or stackoverflow.com for EXC_BAD_ACCESS, you should find plenty of info on how to approach debugging this.
I'm sure that the problem it's the initializing of one or two vars in the code, probably related to name or path where the image.png was stored.
What about my last two questions? What can you tell me about?
OK!!! Solved... the problem was that the const char *picture is the file name of a Picture.png that first you must include in the resources folder of the project.
How can I get a picture.png that It's stored to my Documents Directory? There is a function like CFBundleCopyResourceURL that allows to do this?
OK!!! Solved... the problem was that the const char *picture is the file name of a Picture.png that first you must include in the resources folder of the project.
How can I get a picture.png that It's stored to my Documents Directory? There is a function like CFBundleCopyResourceURL that allows to do this?
Thanks
Finally I use the UIImage that I have in a UIImageView instead of a image stored in my documents directory.
I use this code:
Code:
// This code block will create an image that we then draw to the page
CGImageRef *image=[imageSelected CGImage];
CGContextDrawImage (pdfContext, CGRectMake(200, 200, 207, 385),image);
CGImageRelease (image);
// End image code
The var imageSelected comes from the UIImageView.image;
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.
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.
Hi danielb21,
Thanks for the wonderful tutorial...It helped me alot..
Thanks for the great tutorial. It helps me a lot. Finally I need to display the € currency symbol. In stead of Euro symbol, the pDF file shows some other unrecognized characters. Could you help me in this regard......
thanks,
Ramesh.
Did you ever figure this out? I cannot get euro to work either. Any ideas?