以前在IE下用window.print实现打印由于有active等IE特性的强大功能,打印都比较简单。但是在FF,SAFARI,CHROME,以及MAC操作系统下用js实现打印会出现很多莫名其妙的问题。
1 预览和真实打印效果不同。
在不同操作系统不同浏览器及版本下,这是很正常的。以最终目标打印结果为标准。
2 多页打印的排版错位。
采用table动态生成tr td内容项拼凑出打印内容,但是这种效果即使预览OK了打印出来都可能是错位的。放弃这种做法。
3 连续打印会出现空白页。
连续打印即动态更新打印内容,(排版固定)将新的内容替换旧的内容再依次循环执行window.print。
出现空白页的原因是没有设置page-break-before和page-breake-after为avolid。
4 将web程序嵌入MAC XCOED程序的配置问题。
将MAC下的XCODE程序嵌入web应用,并配置打印设置的配置文件:
#import "mainView.h"
@implementation mainView
@synthesize cursorArea;
- (IBAction)reloadHome:(NSButton *)sender {
[[mainWebView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://access.nmg.com.hk/"]]];
}
-(void)awakeFromNib{
[backGroundView setEnabled:TRUE];
//[mainWebView initWithFrame:[[NSScreen mainScreen]frame]];
[mainWebView autoresizesSubviews];
[mainWebView setFrameLoadDelegate:self];
[mainWebView setPolicyDelegate:self];
[mainWebView setUIDelegate:self];
[mainWebView setEditable:FALSE];
[[mainWebView preferences] setJavaEnabled:TRUE];
[[mainWebView preferences] setJavaScriptEnabled:TRUE];
[[mainWebView preferences] setPlugInsEnabled:TRUE];
[[mainWebView preferences] setUsesPageCache:TRUE];
[[mainWebView preferences] setJavaScriptCanOpenWindowsAutomatically:TRUE];
[[mainWebView preferences] setAllowsAnimatedImages:TRUE];
[[mainWebView preferences] setAllowsAnimatedImageLooping:TRUE];
[[mainWebView preferences] setLoadsImagesAutomatically:TRUE];
[[mainWebView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://access.nmg.com.hk/"]]];
//cursorArea = [[NSTrackingArea alloc] initWithRect:NSMakeRect(0, 0, [[NSScreen mainScreen] frame].size.width, 3) options: (NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways) owner:self userInfo:nil];
//[self addTrackingArea:cursorArea];
}
-(void)webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message
{
NSAlert *alert = [NSAlert alertWithMessageText:nil
defaultButton:@"OK"
alternateButton:nil
otherButton:nil
informativeTextWithFormat:@"%@",message
];
[alert setAlertStyle:NSWarningAlertStyle];
[alert beginSheetModalForWindow:[sender window]
modalDelegate:self
didEndSelector:@selector(endAlert)
contextInfo:nil];
}
-(void)endAlert
{
NSLog(@"end");
}
- (void)moveTrackingAreaup {
[super updateTrackingAreas];
[self removeTrackingArea:cursorArea];
cursorArea = [[NSTrackingArea alloc] initWithRect:NSMakeRect(0, 0, [[NSScreen mainScreen] frame].size.width, 60) options: (NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways) owner:self userInfo:nil];
[self addTrackingArea:cursorArea];
}
-(void)moveTrackingAreadown{
[super updateTrackingAreas];
[self removeTrackingArea:cursorArea];
cursorArea = [[NSTrackingArea alloc] initWithRect:NSMakeRect(0, 0, [[NSScreen mainScreen] frame].size.width, 3) options: (NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways) owner:self userInfo:nil];
[self addTrackingArea:cursorArea];
}
/*- (void)mouseEntered:(NSEvent *)event
{
[[mainWebView animator] setFrameOrigin:NSMakePoint([mainWebView frame].origin.x,60)];
[[buttonControlView animator] setAlphaValue:1];
//if ([backGroundView isEnabled] == FALSE) {
// [backGroundView setEnabled:TRUE];
// }
[self performSelector:@selector(moveTrackingAreaup)];
}*/
-(void)hideFunction{
if ([backGroundView isEnabled] == TRUE) {
[backGroundView setEnabled:FALSE];
}
}
/*- (void)mouseExited:(NSEvent *)event{
[self performSelector:@selector(moveTrackingAreadown)];
[[mainWebView animator] setFrameOrigin:NSMakePoint([[NSScreen mainScreen] frame].origin.x,[[NSScreen mainScreen] frame].origin.y)];
[[buttonControlView animator] setAlphaValue:0];
// [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(hideFunction) userInfo:nil repeats:NO];
}*/
//-(void)dealloc{
// [super dealloc];
//}
#pragma mark - WebUIDelegate
- (void)webView:(WebView *)sender printFrameView:(WebFrameView *)frameView
{
if ([frameView documentViewShouldHandlePrint]) {
[frameView printDocumentView];
} else {
NSPrintInfo *printInfo;
NSPrintOperation *printOp;
NSMutableDictionary *printInfoDict;
printInfoDict = [NSMutableDictionary dictionaryWithDictionary:
[[NSPrintInfo sharedPrintInfo] dictionary]];
[printInfoDict setObject:NSPrintSpoolJob forKey:NSPrintJobDisposition];
printInfo = [[NSPrintInfo alloc] initWithDictionary: printInfoDict];
// [printInfo setHorizontalPagination: NSFitPagination];
// [printInfo setVerticalPagination: NSFitPagination];
[printInfo setHorizontalPagination: NSClipPagination];
[printInfo setVerticalPagination: NSClipPagination];
// [printInfo setVerticallyCentered:NO];
[printInfo setOrientation:NSLandscapeOrientation];
[printInfo setLeftMargin:-5];
[printInfo setTopMargin:0];
[printInfo setRightMargin:0];
[printInfo setBottomMargin:-30];
[printInfo setPaperSize:NSMakeSize(120, 240)];
[printInfo setScalingFactor:1];
printOp = [NSPrintOperation printOperationWithView:[frameView documentView]
printInfo:printInfo];
[printOp setShowsPrintPanel:NO];
[printOp runOperation];
}
}
@end
5 内容采用下拉框(动态显示)和直接显示(静态)的打印效果不同
采用下拉框动态显示会出现如果打多页第一页就是空白页。而静态不会。(后来证实,只有6页以上才不会,2-5页都会有空白页)。同样是page-breake的问题。
6 显示打印内容隐藏不打印内容