How can I create a PDF file programmatically in an iOS application? - ios

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.

Related

Excel to pdf conversion in rails4 with libreoffice

In a rails 4 application, how can I convert an excel spreadsheet file into pdf.
When I am trying to implement Excel to pdf the contents are listed in different pages if excel column size is large.
How to generate the pdf without moving data to next pages in pdf.
Please help,
Thanks
So you basically have two options you can either implement this yourself and use a CSV gem/library (default CSV, faster CSV, or smarter CSV) assuming that by "excel" a CSV is acceptable. If a CSV is not acceptible you can use the axlsx gem instead. Then for pdf conversion you can use something like prawn. If you decide to build this yourself follow these steps.
Create a controller that will handle Reports, I suggest using the rails g controller report upload generate_table show generate_pdf generator to create a controller and a view for the upload process
Create a file upload form in the upload view.
On submit you will send the file to the generate action processing with one of the CSV or excel gems
Once processed your end product should be an array or hash (as an instance variable) and you can send that to the show action
In the show view you will iterate of that hash/array and incapsulate the contents in a html table.
On the show view you should have a button that will send that same hash/array to the generate_pdf controller action where you will use prawn to create a pdf, you can use something like send_data to the send the completed pdf file back to the user.
This is roughly how you could go about it less the low level details. Now if you wanted to use an out of the box solution you could use something like Ruport. Ruport will handle most of the heavy lifting for you the only thing is you need to have your models and associations set up to use it the way it is designed, and that may not be an option for you.

PrawnPDF Flip Entire PDF

I'm using Prawn PDF to create a label that I send to a label printer, but the label prints upside down. This is important as the shipping labels we use come with some print already on it. The setup I'm using (an iPad through a Lantronix xPrintServer to a Zebra Printer) won't allow me to flip it using the drivers.
So I'm wanting to know if there is a way using Prawn (or even just Rails) to flip the entire document (which contains 2+ pages) so it prints out correctly on the labels. The order of the pages isn't essential.
I haven't used Prawn lately, but I'm pretty sure using the rotate method at the top of your code will work. You'll just need to either set the origin to the center of the page, or use translate to reposition the content after rotation. Page 29 in the manual (PDF) has some example code.
You could save the pdf to a file and then use the awesome pdftk to rotate the saved pdf, then send the amended version.
https://www.pdflabs.com/docs/pdftk-cli-examples/
EDIT - pdftk is not a library/plugin/gem, or any kind of Ruby for that matter. It's a command line tool which you would use like this, in your controller, replacing your current "generate and send pdf" code.
#instead of sending the pdf straight to the user, save it to a file
#i'm not sure how to do this in prawn but it can't be difficult
#rotate the original to a new file
`pdftk /path/to/original.pdf cat 1-endsouth output /path/to/rotated.pdf`
#you could test whether the rotated file exists here as an error-check
#then use send_file to send the rotated one as the response.
send_file "/path/to/rotated.pdf", :type => "application/pdf"

ios creating multiple-page pdfs

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.

open source controls to convert rich text formatted code to html markup

I am working on asp.net mvc. I am trying to display the rich text formatted content like,
{\rtf1\ansi\ansicpg1252\uc1\htmautsp\deff2{\fonttbl{\f0\
fcharset0 Times New Roman;}{\f2\fcharset0 Tahoma;}}{\colortbl\red0\green0\blue0;\red255\green255\blue255;}\loch\hi
ch\dbch\pard\plain\ltrpar\itap0{\lang1033\fs24\f2\cf0 \cf
0\ql{\f2 {\ltrch AMANDA WITH RC CALLED AND WANTED TO
VERIFY THAT WE WERE AFFILIATED WITH SHAUN # JAGGYS. LET HER KNOW WE
WERE, SHAUN CALLED RC AS WELL TO VERIFY STATUS OF BD}\li0\ri0\sa0\sb0\fi0\ql\par}
}
}
in the view. Actually this data could come from database table and i need to display it in the editor type control. so is there any open source controls that are able to display rich text format.
Well, I just got done writing a RTF to HTML converter that maintains all embedded media, and creates a MIME multipart message out of it. This is close to what you want to do. Essentially if you aren't interested in writing your own converter, you can look at this CodeProject and use his: http://www.codeproject.com/Articles/27431/Writing-Your-Own-RTF-Converter
There is also descriptions as to how to reach his solution.
On my project we just started ripping apart the RTF document and parsing its contents. Open source and 3rd-Party Libraries weren't an option for me.

Create pdf inapp iOS [duplicate]

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.

Resources