Skip navigation.
 
mlEPS NSPrintOperation not working as set
FROM : Warwick Hall
DATE : Mon Apr 30 08:55:37 2007

i know i have bugged the list alot over the last few days, but your 
help is greatly appreciated.

lately i have had trouble getting NSPrintOperation to work correctly.

background: i am writing a command line tool that renders web HTML in 
a webView and prints the result to a postscript file (via nsdata, see 
below).

problem: NSPrintInfo ignores and alters my settings for no apparent 
reason. even when the NSPrintInfo seems to initialize correctly, 
NSPrintOperation ignores it! for instance, using NSLog ( [printInfo 
description] ) and looking at the final output i can deduce the 
following:

1) setting the margins is acceptable to NSPrintInfo, but 
NSPrintOperation pays no attention.
2) same with vertical and horizontal pagination.
3) same with page type and page size.
4) setting the job type to NSPrintSaveJob will be accepted by 
NSPrintInfo, but get this: it erases the path i give it from the 
dictionary. where is the logic in that???? moreover it refuses to 
allow me to use the printOperation: ... toPath:, instead i have to 
use the toData: message, and use the [data writeToFile:atomically:] 
to get it to the filesystem. i am sorry, but there must be some kind 
of bug there!

Finally, it appears that all printOperation does in the end is print 
the visible rect of the webView. ie it completely ignores all 
printInfo settings like pagination and margins, and all the others.

--BEGIN--
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#import "webFrameDelegate.h"

void gbGenOrder( NSString * iCid, NSString * iWid, NSString * iRid, 
NSString * iRdate,
  NSString * iOutput )
{
  int loop = 0;
  NSString * theHtml = @"<html><body>H<br/>e<br/>l<br/>l<br/>o<br/
>W<br/>o<br/>r<br/>l<br/>d<br/>!<br/>Hello 0<br/>H<br/>e<br/>l<br/
>l<br/>o<br/>W<br/>o<br/>r<br/>l<br/>d<br/>!<br/>Hello 1<br/>H<br/
>e<br/>l<br/>l<br/>o<br/>W<br/>o<br/>r<br/>l<br/>d<br/>!<br/>Hello 

2<br/>H<br/>e<br/>l<br/>l<br/>o<br/>W<br/>o<br/>r<br/>l<br/>d<br/>!
<br/>Hello 3<br/>H<br/>e<br/>l<br/>l<br/>o<br/>W<br/>o<br/>r<br/>l<br/
>d<br/>!<br/>Hello 4<br/>H<br/>e<br/>l<br/>l<br/>o<br/>W<br/>o<br/
>r<br/>l<br/>d<br/>!<br/>Hello 5<br/>H<br/>e<br/>l<br/>l<br/>o<br/
>W<br/>o<br/>r<br/>l<br/>d<br/>!<br/>Hello 6<br/>H<br/>e<br/>l<br/
>l<br/>o<br/>W<br/>o<br/>r<br/>l<br/>d<br/>!<br/>(THE END!)</body></

html>";

  NSRect bounds = NSMakeRect( 0.0, 0.0,
                              595.200012, 841.919983 );

  NSWindow * theWindow =
    [[NSWindow alloc] initWithContentRect : bounds
                                styleMask : NSBorderlessWindowMask | 
NSUnscaledWindowMask
                                  backing : NSBackingStoreNonretained
                                    defer : NO
                                    screen : nil];

  [NSGraphicsContext setCurrentContext :
    [NSGraphicsContext graphicsContextWithWindow : theWindow]];

  NSRunLoop * theRunLoop = [NSRunLoop currentRunLoop];

  WebView * webView =
    [[WebView alloc] initWithFrame : bounds
        frameName : nil
        groupName : nil];

  webFrameDelegate * delegate = [[webFrameDelegate alloc] init];

  [webView setFrameLoadDelegate : delegate];
  [[[webView mainFrame] frameView] setAllowsScrolling : NO];
  [[theWindow contentView] addSubview : webView];

  [webView lockFocus];
  [[webView mainFrame] loadHTMLString : theHtml baseURL : nil ];
  [webView unlockFocus];

  BOOL isRunning = YES;
  while( isRunning )
  {
    isRunning =
      [theRunLoop runMode : NSDefaultRunLoopMode
                beforeDate : [NSDate dateWithTimeIntervalSinceNow : 
0.5]];

    if( isRunning )
      isRunning = [delegate isRunning];
  }

  NSPrintInfo * printInfo = nil;
  NSPrintInfo * sharedInfo = nil;
  NSPrintOperation * printOp = nil;
  NSMutableDictionary * printInfoDict = nil;
  NSMutableDictionary * sharedDict = nil;

  sharedInfo = [NSPrintInfo sharedPrintInfo];
  sharedDict = [sharedInfo dictionary];

  printInfoDict = [NSMutableDictionary dictionaryWithDictionary : 
sharedDict];

  [printInfoDict setObject : @"iso-a4"
                    forKey : NSPrintPaperName];

  [printInfoDict setObject : [NSValue valueWithSize :
                                NSMakeSize( 595.200012, 841.919983 ) ]
                    forKey : NSPrintPaperSize];

  [printInfoDict setObject : [NSNumber numberWithFloat : 1.0]
                    forKey : NSPrintScalingFactor];

  NSNumber * marginPoints = [NSNumber numberWithFloat : 10.0];
  [printInfoDict setObject : marginPoints
                    forKey : NSPrintTopMargin];

  [printInfoDict setObject : marginPoints
                    forKey : NSPrintRightMargin];

  [printInfoDict setObject : marginPoints
                    forKey : NSPrintLeftMargin];

  [printInfoDict setObject : marginPoints
                    forKey : NSPrintBottomMargin];

  [printInfoDict setObject : [NSNumber numberWithBool : YES]
                    forKey : NSPrintAllPages];

  [printInfoDict setObject : NSPrintSaveJob
                    forKey : NSPrintJobDisposition];

  [printInfoDict setObject : iOutput
                    forKey : NSPrintSavePath];

  printInfo = [[NSPrintInfo alloc] initWithDictionary : printInfoDict];

  [printInfo setHorizontalPagination : NSAutoPagination];
  [printInfo setVerticalPagination : NSAutoPagination];
  [printInfo setHorizontallyCentered : YES];
  [printInfo setVerticallyCentered : NO];

  NSMutableData * theData = [NSMutableData data];
  printOp = [NSPrintOperation EPSOperationWithView : webView
                                        insideRect : bounds
                                            toData : theData
                                          printInfo : printInfo];
  [printOp setShowPanels : NO];

  [printOp runOperation];
  [printOp deliverResult];

  [theData writeToFile : iOutput atomically : NO];

  [theHtml release]; theHtml = nil;
  [delegate release]; delegate = nil;
}
--END--

Related mailsAuthorDate
No related mails found.