Re: Creating pdf document
Re: Creating pdf document
- Subject: Re: Creating pdf document
- From: Jonathan Mitchell <email@hidden>
- Date: Sat, 16 Aug 2014 17:12:00 +0100
On 16 Aug 2014, at 11:18, Jean Suisse <email@hidden> wrote:
> Dear All,
>
> I have a goods management cocoa application which needs to generate pdf documents. It isn’t a document-based app, it doesn’t use core data and the data that should be printed are not laid out in any of the views displayed to the user.
>
> In short, I would need to generate a pdf document with nice headers and footers on each page, a title and some information on the first page, then a multi-page section containing some of the database displayed as a table, followed by a summary with some text and numbers at the end.
>
> I have little experience with the cocoa printing system (basically, I know how to print a view) and the main issues I face are :
>
> – alignment of the text (left, right, …) on the page… I would like to select the paper format and the orientation. This is not an issue since the app development is strongly tied to laws and regulation, limiting the user’s choice to A4 paper, both orientations, is acceptable.
> – drawing the table so that it is well cut at the bottom of the page. If possible I would like to replicate the header of the table on each new page.
>
> Before I even start developing this part of the app, what advices would you give me to speed up the process
> Is there some kind of template system that I can use, to visually build the document and define the position of the cotent?
Unfortunately not
> Should I build a view, then print it ?
I would say it depends on the complexity of the desired output.
> Is it more efficient to work directly on the pdf?
In my case I had to target a complex official pdf.
Creating an actual NSView would have been tedious and fragile.
It was much easier to use a existing pdf generated in say InDesign and write the required text to it.
I would:
Declare a PDFDocument subclass.
In the document - (Class)pageClass return a custom PDFPage class.
You may have to be creative with PDFPage initialiser : http://lists.apple.com/archives/cocoa-dev/2014/Feb/msg00172.html
The PDFPage subclass is your ticket to writing to a PDF page loaded with PDFDocument -initiWithURL:
The PDFPage uses an invariant bottom left 72 dpi co-ordinate system : https://developer.apple.com/library/Mac/documentation/GraphicsImaging/Conceptual/PDFKitGuide/PDFKit_Prog_Conc/PDFKit_Prog_Conc.html#//apple_ref/doc/uid/TP40001863-CH201-CHDIGGFD
PDFPage -- (void)drawWithBox:(PDFDisplayBox)box is the method in which to do your text drawing using normal Cocoa API:
e.g.: [self.text drawWithRect:self.rect options:NSStringDrawingUsesLineFragmentOrigin attributes:self.attributes];
In our case we simply measured the desired output positions of our text and created an XML map of the required output positions.
In -drawWithBox we iterate over the map and write text into the page.
When printing it seems best to target the PDFView -documentView.
There are two points to note when it comes to outputting the modified PDF to file.
1. To have the saved PDF paginated use an NSPrintInfo object. Set the -jobDIsposition to NSPrintSaveJob.
2. To output the PDF unpaginated get an NSData instance from PDFDocument -dataRepresentationWithOptions; and write to file.
Jonathan
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden