Ian Turner
Ian Turner

Reputation: 1413

Problems creating a Webview for printing

I'm trying to use a Webview to add some text around a graph produced using the Core Plot framework for printing. I have the following code which for the most part seems to work, however it is not displaying an image but the question mark image for a missing image. Can anyone point me in the right direction for what the problem might be:

NSData        *imageData = [[barChart imageOfLayer] TIFFRepresentation];
NSString      *temporaryImagePath = NSTemporaryDirectory();
NSError       *error = nil;

NSBitmapImageRep*   imageRep    = [NSBitmapImageRep imageRepWithData:imageData];
NSData*             pngData;

pngData = [imageRep representationUsingType:NSPNGFileType 
                                 properties:nil];

temporaryImagePath = [temporaryImagePath stringByAppendingPathComponent:@"pngData.png" ];
[pngData writeToFile:temporaryImagePath 
               options:NSDataWritingAtomic 
                 error:&error ];

WebView *printView;

printView =[[WebView alloc] initWithFrame:NSMakeRect(0, 0, 300, 500)
                                frameName:@"printFrame" 
                                groupName:@"printGroup"];


NSMutableString *theURLString = [[NSMutableString alloc] initWithString:@"<html>"];
[theURLString appendString:@"<head>"];
[theURLString appendString:@"</head>"];

[theURLString appendString:@"<body>"];
[theURLString appendString:[NSString stringWithFormat:@"<img src=\"%@\" alt=\"Histogram\"/>", temporaryImagePath]];
[theURLString appendString:@"</body></html>"];

[[printView mainFrame] loadHTMLString:theURLString
                  baseURL:[NSURL URLWithString:@"a URL"]];
[[[[printView mainFrame] frameView] documentView] print:sender];
[printView release];

Upvotes: 0

Views: 532

Answers (1)

omz
omz

Reputation: 53551

You're using a regular path as the source of the image, which isn't valid HTML. You should use a file:// URL, like this:

//...
NSURL *tempImageURL = [NSURL fileURLWithPath:temporaryImagePath];
[theURLString appendFormat:@"<img src=\"%@\"/>", [tempImageURL absoluteString]];
//...

Upvotes: 2

Related Questions