EPSOperationWithView toPath not working
EPSOperationWithView toPath not working
- Subject: EPSOperationWithView toPath not working
- From: Warwick Hall <email@hidden>
- Date: Tue, 24 Apr 2007 09:44:29 +1000
hello all,
i actually mistakenly sent this post to the carbon-dev list. oops!
anyway...
i am writing a command line tool with cocoa, which will download html
from a url, and save it in a paginated postscript file. so here is my
strategy
1.) create a url request with POST data and method
2.) Use NSConnection to synchronously download the html content and
convert this to NSString.
-- seems to work fine to this point! --
3.) Create a WebView object and load it with the html string from above.
5.) set the appropriate properties of a printInfo object which states
pagination and destination of output (a file)
4.) Create an EPSPrintOperation with the webView object and printInfo
and "runOperation" it
5.) expect it to work!
There seems to be a problem with step 5. no file is outputted. the
runOperation message returns YES. i have commented out the
NSSavePrintJob property setting, but with or without it it still does
not work.
i include my code below.
warwick
---BEGIN---
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
void usage ( prog )
const char * prog;
{
fprintf( stderr,
"Usage: %s <cid> <wid> <rid> <rdate> <file>\n",
basename( prog ) );
}
NSString * gbUrlEncodeValue ( NSString * iValue )
{
NSString * theRet = nil;
theRet = (NSString *)CFURLCreateStringByAddingPercentEscapes(
kCFAllocatorDefault, (CFStringRef)iValue, NULL,
CFSTR( "?=&+" ), kCFStringEncodingUTF8 );
return [theRet autorelease];
}
void gbGenOrder( NSString * iVal1, NSString * iVal2, NSString *
iVal3, NSString * iVal4,
NSString * iOutput )
{
NSString * thePostString =
[NSString stringWithFormat : @"key1=%@&key2=%@&key3=%@&key4=%
@&key5=%@",
gbUrlEncodeValue( (NSString *)CFSTR( "dsadfgsyw" ) ),
gbUrlEncodeValue( iVal1 ),
gbUrlEncodeValue( iVal2 ),
gbUrlEncodeValue( iVal3 ),
gbUrlEncodeValue( iVal4 )];
NSData * thePostData =
[thePostString dataUsingEncoding : NSASCIIStringEncoding
allowLossyConversion : YES];
NSString * thePostLength =
[NSString stringWithFormat : @"%d", [thePostData length]];
NSMutableURLRequest * theRequest = [[NSMutableURLRequest alloc]
init];
[theRequest setURL :
[NSURL URLWithString : @"https://somewhere/script.php"]];
[theRequest setHTTPMethod : @"POST"];
[theRequest setValue : thePostLength
forHTTPHeaderField : @"Content-Length"];
[theRequest setValue : @"application/x-www-form-urlencoded"
forHTTPHeaderField : @"Content-Type"];
[theRequest setHTTPBody : thePostData];
NSURLResponse * theResponse = nil;
NSError * theError = nil;
NSData * theHtmlData =
[NSURLConnection sendSynchronousRequest : theRequest
returningResponse : &theResponse
error : &theError];
if( theError != nil )
{
[theRequest release]; theRequest = nil;
return;
}
if( theHtmlData == nil )
{
NSLog( @"theHtmlData is nil" );
[theRequest release]; theRequest = nil;
return;
}
NSString * theHtml =
[[NSString alloc] initWithBytes : [theHtmlData bytes]
length : [theHtmlData length]
encoding : NSASCIIStringEncoding];
WebView * theView =
[[WebView alloc] initWithFrame : NSMakeRect( 0.0, 0.0, 300.0,
500.0 )
frameName : nil
groupName : nil];
[[theView mainFrame] loadHTMLString : theHtml baseURL : nil ];
[theView setNeedsDisplay : YES];
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 : [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 setVerticallyCentered : NO];
printOp = [NSPrintOperation EPSOperationWithView : theView
insideRect : NSZeroRect
toPath : iOutput
printInfo : printInfo];
[printOp setShowPanels : NO];
[printOp runOperation];
[theHtml release]; theHtml = nil;
[theView release]; theView = nil;
[theRequest release]; theRequest = nil;
}
int main ( argc, argv )
const int argc;
const char * const * argv;
{
if( argc != 6 )
{
usage( basename( argv[ 0 ] ) );
exit( EXIT_FAILURE );
}
NSAutoreleasePool * thePool = [[NSAutoreleasePool alloc] init];
[NSApplication sharedApplication];
gbGenOrder( [NSString stringWithCString : argv[ 1 ]],
[NSString stringWithCString : argv[ 2 ]],
[NSString stringWithCString : argv[ 3 ]],
[NSString stringWithCString : argv[ 4 ]],
[NSString stringWithCString : argv[ 5 ]] );
[thePool release]; thePool = nil;
exit( EXIT_SUCCESS );
}
---END---
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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