Re: Creating pdf document
Re: Creating pdf document
- Subject: Re: Creating pdf document
- From: Jean Suisse <email@hidden>
- Date: Sat, 16 Aug 2014 21:40:24 +0200
Thank you Jonathan Mitchell, Edward Taffel, and Joel Norvell for your replies.
I asked the question before catching a train, hoping to have some thoughts to read when arriving home. However, during the 3.5 hour-long journey, I had some time to think about it and found a simple solution: I realized that the king of the layout is probably is a web browser and that a web browser can print content really well.
The solution isn’t not pure cocoa, but it works pretty well for me and solves all the issues I had, especially about the layout and the template. That said, I am still interested by reading your thoughts (though I will probably keep this solution anyway because it gives me the option to offer template edition features to the user).
So I made an HTML template to be loaded in a web view :
> [webView setFrameLoadDelegate: self];
> [[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:templateURL]]];
Then the web view is printed (hitting have as pdf does the job):
- (void)webView:(WebView*)webView didFinishLoadForFrame:(WebFrame *)frame
{
NSPrintInfo *printInfo = [NSPrintInfo sharedPrintInfo];
[printInfo setPaperSize:NSMakeSize(595.22, 841.85)];
[printInfo setTopMargin:0.0];
[printInfo setLeftMargin:0.0];
[printInfo setRightMargin:0.0];
[printInfo setBottomMargin:0.0];
NSPrintOperation *printOp = [NSPrintOperation printOperationWithView:[[[printView mainFrame] frameView] documentView] printInfo:printInfo];
[printOp setShowsPrintPanel: YES];
[printOp runOperation];
}
It took me some time to figure out that the printing had to be done in didFinishLoadForFrame – I am no webkit expert.
Code is ARC, and reveals a non-programming related “issue” I have had for a long time using preview: if the background is transparent, then it is represented by two-color squares in preview. The document displayed is ugly and I can barely read the text. So to create this document, I used 0 margins, and wrapped the html in a DIV with the appropriate padding to emulate the margin (could have done so with margin too probably). Just remove the lines about the margins to see what I am talking about (content is on a white background surrounded by an ugly transparent margin).
Thank you for your feedback,
Jean
On Aug 16, 2014, at 19:25 , Joel Norvell <email@hidden> wrote:
> Hi Jonathan,
>
> Thanks for the great PDFKit layout recipe on cocoa-dev, today!
>
> I've done a number of things with PDFKit, but not the type of layout you described.
>
> So now, if I ever need to, I'll have a path to follow :-)
>
> Gratefully yours,
> Joel
>
> P.S. And thank you Jean Suisse for asking such an interesting question!
On Aug 16, 2014, at 18:50 , edward taffel <email@hidden> wrote:
> a cocoa solution may yet be forthcoming, but—if you enjoy pdf, qua pdf, i should investigate CGPDFContextCreate.
>
> regards,
> edaward
On Aug 16, 2014, at 18:12 , Jonathan Mitchell <email@hidden> wrote:
>
> 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