How would I go about creating a multiple page pdf from a scrolling view? I am trying to convert an invoice pdf and it is much longer than 1 page; not sure how to create more.
also while on this, the pdf also seems to be quite choppy in quality ( very rasterized ) - any tips to improve this? i realize it's not vectorized, hence the choppy look, just looking for a way to make it better? thanks
To create multiple pdf, after creating each page you can convert the entire page to an image an store it in an array, one by one. Then you can show each image from the array as scrollview if thats what you meant.
You can create pdf pages and write it to a file with following function. Call it for each page.
-(void)savePDFImageToDocumentDirectory:(UIImage *)pdfImage withPageNumber:(int )pageNumber{
NSString *tmpPngPath = PDFTempDirectoryPath;
NSString *pageName = [NSString stringWithFormat:#"PDFPage%d.png",pageNumber];
NSString *imagePath = [tmpPngPath stringByAppendingPathComponent:pageName];
[UIImagePNGRepresentation(pdfImage) writeToFile:imagePath atomically:YES];
}
You can follow two approaches tom create pdf. One through html method and the other is from xib files. Html method is fast but pdf will be of less quality. xib method gives you high quality pdf but uses a lot of memory.
If you are happy for the PDF generation to be remote you could use a service like Docmosis cloud services or Aspose to create the Document and stream it back to the device (or email it or both etc).
Typically these PDF tools wouldn't build documents from images like screen shots - you would send the data you want populated instead. This means the quality is very good (and text is selectable etc) and multiple pages is expected making the PDFs more generally useful.
Please note I work for the company that created Docmosis.
Hope that helps.
Related
Problem1 : I have a PDF document containing 1300 pages. Users are able to add and remove pages from it. If I want to store the new PDF Document I have to convert it to data with (.dataRepresentation() ). This takes about 10 seconds which is way to long (What if the user terminates the app).
Solution: Split the document in 1300 PDF's, each one containing 1 page. Store this PDF inside an array called var pages = Data. Users can now add and remove new Pages from this array easily. I don't have to call .dataRepresentation() anymore.
Problem 2: When I use this method i have a new problem. The memory for this array is very huge. About 400mb. The PDF document itself is only 46mb.
How can i solve this problem ? Is there maybe another way ? Please help me out i can't find any solutions :(
I would like to create a XPages form to print it out. I am not sure which is the best way to do this. I mean I need to create a panel or table or any other design elements then put all my staff in it?
I need advise:)
Regards
Cumhur Ata
Speak after me: browsers don't print.
If you need precision layout a browser is your worst enemy. Each one renders slightly different, so don't bother.
Your best bet is to create a PDF file for printing. There you have pixel-perfect rendering as you deem fit.
You can use iText, PDFBox or XSL:FO to generate PDF. There are code samples on OpenNTF or you read my blog entries about it.
I have a table in SQLlite on my iPad app. When the user clicks a button "Print" i want to call a method which would read the table and create a PDF file with the table data. Also want to add the company logo and address on top.
Any idea how this can be done? I'm a beginner in iOS programming and hence do not know where to start. Any help is appreciated.
Thanks
I have recently been working on an app which required PDFs to be produced for emailing/printing.
For this I used NDHTMLtoPDF which as its name suggests generates a PDF from HTML. To accomplish the HTML generating I have a base HTML template in the apps main bundle which has custom tags such as {content} which I can then replace with whatever I want within my class by using
stringByReplacingOccurrencesOfString:#"{content}" withString:MY_HTML_STRING
so, within your base HTML file you would already have the company logo and address set as well as the relevant table tags.
I appreciate you are new to iOS Programming, so I suggest you look into the NSString methods available for adding and replacing strings.
Update:
First you need to get your baseHtml file into a NSString:
NSString *htmlString = [[NSBundle mainBundle] pathForResource:#"base" ofType:#"html"];
As mentioned, the HTML file would already have tags to place the company logo and address in the required positions on the page.
You would then have HTML tags which produces a table, which would be designed based on how you want your model data to be displayed.
Within the table, you would have various placeholder tags which I usually enclose in {}, however you can use <> or whatever you like, which you would use to populate with your model data.
So, say you had an attribute in your model which was firstName, you would place {firstName} in one of the cells within a HTML table cell.
Then back in the class where you are generating your HTML, you would do:
htmlString = [htmlString stringByReplacingOccurrencesOfString:#"{firstName}" withString:[YOUR_MODEL firstName]];
You would then do that for each of the data model objects you wish to display in the PDF.
How to generate a PDF file on a user action?
See "Drawing with Quartz" to see how to create a PDF Graphics Context.
Some notes:
"iPhoneOSNote: If you want to create a PDF graphics context in an iPhone application, make sure you also read “Drawing to a Graphics Context in iPhone OS” (page 27)."
"You can write any content to a PDF that’s appropriate for your application—images, text, path drawing—and you can add links and encryption. For more information see “PDF Document Creation, Viewing, and Transforming” (page 177)."
As an example of creating a PDF from the contents of a CALayer, you can refer to the -dataForPDFRepresentationOfLayer method within the CPLayer class in the Core Plot framework. You can do something similar to extract the content from a CALayer-backed UIView, although we had to subclass CALayer in order to get vector elements to render to a PDF properly.
Use this library to generate pdf file by programmatically.
SJ_PDFCreator
Use a web service on a server back end to post the data to - then generate the PDF and return it to the browser/app with the correct mime type to open as a PDF.
ColdFusion could easily do this for you in just a few lines of code.
How to generate a PDF file on a user action?
See "Drawing with Quartz" to see how to create a PDF Graphics Context.
Some notes:
"iPhoneOSNote: If you want to create a PDF graphics context in an iPhone application, make sure you also read “Drawing to a Graphics Context in iPhone OS” (page 27)."
"You can write any content to a PDF that’s appropriate for your application—images, text, path drawing—and you can add links and encryption. For more information see “PDF Document Creation, Viewing, and Transforming” (page 177)."
As an example of creating a PDF from the contents of a CALayer, you can refer to the -dataForPDFRepresentationOfLayer method within the CPLayer class in the Core Plot framework. You can do something similar to extract the content from a CALayer-backed UIView, although we had to subclass CALayer in order to get vector elements to render to a PDF properly.
Use this library to generate pdf file by programmatically.
SJ_PDFCreator
Use a web service on a server back end to post the data to - then generate the PDF and return it to the browser/app with the correct mime type to open as a PDF.
ColdFusion could easily do this for you in just a few lines of code.