非IE MAC平台下window.print出现空白页的解决办法

以前在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 显示打印内容隐藏不打印内容

使用两个css文件。一个main.css一个print.css。
<link href="style/main.css" rel="stylesheet" type="text/css" media="screen"/>
<link href="style/print.css" rel="stylesheet" type="text/css" media="print"/>

打印时使用print.css。在print.css中将要隐藏的内容都隐藏了,要打印的显示出来。这时会显示打印的内容会使页面看起来比较乱。但是如果是直接打印不需要预览则不会看到(速度快)。预览的话还是建议新窗口显示预览结果。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值