Paste number 18505: Trying to turn a WebView into paginated PDF in memory

Index of paste annotations: 1

Paste number 18505: Trying to turn a WebView into paginated PDF in memory
Pasted by: zorn
When:18 years, 11 months ago
Share:Tweet this! | http://paste.lisp.org/+EA1
Channel:None
Paste contents:
Raw Source | XML | Display As
- (IBAction)makePDF:(id)sender
{
	// the follow method is meant to convert a webview in a multipage PDF.
	// for our app we need it in memory, but to date have only got it working 
	// when we generate a file.
	
	// what I want is to be able to set shouldGenrateFile to NO and have 
	// it generate the PDF properly in memory so I can display it.
	
	NSLog(@"Make PDF...");
	
	// if this is true we'll use the print system to generate a file
	// else we'll generate an in memory pdf to display.
	BOOL shouldGenrateFile = YES; 
	
	NSMutableData *data = [NSMutableData data];
	NSPrintOperation *printOp;
	NSPrintInfo *printInfo;
	NSPrintInfo *sharedInfo;
	NSMutableDictionary *printInfoDict;
	NSMutableDictionary *sharedDict;
	
	sharedInfo = [NSPrintInfo sharedPrintInfo];
	sharedDict = [sharedInfo dictionary];
	printInfoDict = [NSMutableDictionary dictionaryWithDictionary:
		sharedDict];
	
	if (shouldGenrateFile) {
		[printInfoDict setObject:NSPrintSaveJob forKey:NSPrintJobDisposition];
		[printInfoDict setObject:@"/Users/zorn/Desktop/web.pdf" forKey:NSPrintSavePath];
	}
	
	printInfo = [[NSPrintInfo alloc] initWithDictionary: printInfoDict];
	[printInfo setHorizontalPagination: NSAutoPagination];
	[printInfo setVerticalPagination: NSAutoPagination];
	[printInfo setVerticallyCentered:NO];
	
	NSView *viewToPrint = [[[webView mainFrame] frameView] documentView];

	if (shouldGenrateFile) {
		// if we 
		printOp = [NSPrintOperation printOperationWithView:viewToPrint 
															printInfo:printInfo];
		[printOp setShowPanels:NO];
		[printOp runOperation];
	} else {
		printOp = [NSPrintOperation PDFOperationWithView:viewToPrint
														   insideRect:[viewToPrint frame]
															   toData:data
															printInfo:printInfo];
		
		[printOp runOperation];
		
		[pdf release];
		pdf = [[PDFDocument alloc] initWithData:data];
		[pdfView setDocument:pdf];
	}
}

Annotations for this paste:

Annotation number 1: How to punt
Pasted by: boredzo
When:18 years, 10 months ago
Share:Tweet this! | http://paste.lisp.org/+EA1/1
Paste contents:
Raw Source | Display As
You could use a RAM disk.

Step 1: hdid -nomount ram://NUM_SECTORS (sector = 512 bytes)
This outputs a device path (/dev/foo).
Step 2: newfs_hfs -v VOLUME_NAME DEVICE_PATH
Formats the RAM disk as HFS+. See x-man-page://newfs_hfs for other options.
Step 3: diskutil mount DEVICE_PATH

The RAM disk will be mounted at /Volumes/VOLUME_NAME. You could also do something involving mount(8) (x-man-page://8/mount) if you wanted to mount it inside NSTemporaryDirectory(). Remember to unmount it (hdiutil detach DEVICE_PATH) when you're done.

Sorry I'm late with this. Didn't see it before.

Colorize as:
Show Line Numbers

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.