Render HTML to an image

后端 未结 17 1797
栀梦
栀梦 2020-11-22 16:06

Is there a way to render html to image like PNG? I know that it is possible with canvas but I would like to render standard html element like div for example.

17条回答
  •  心在旅途
    2020-11-22 16:28

    You can't do this 100% accurately with JavaScript alone.

    There's a Qt Webkit tool out there, and a python version. If you want to do it yourself, I've had success with Cocoa:

    [self startTraverse:pagesArray performBlock:^(int collectionIndex, int pageIndex) {
    
        NSString *locale = [self selectedLocale];
    
        NSRect offscreenRect = NSMakeRect(0.0, 0.0, webView.frame.size.width, webView.frame.size.height);
        NSBitmapImageRep* offscreenRep = nil;      
    
        offscreenRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:nil
                                                 pixelsWide:offscreenRect.size.width
                                                 pixelsHigh:offscreenRect.size.height
                                                 bitsPerSample:8
                                                 samplesPerPixel:4
                                                 hasAlpha:YES
                                                 isPlanar:NO
                                                 colorSpaceName:NSCalibratedRGBColorSpace
                                                 bitmapFormat:0
                                                 bytesPerRow:(4 * offscreenRect.size.width)
                                                 bitsPerPixel:32];
    
        [NSGraphicsContext saveGraphicsState];
    
        NSGraphicsContext *bitmapContext = [NSGraphicsContext graphicsContextWithBitmapImageRep:offscreenRep];
        [NSGraphicsContext setCurrentContext:bitmapContext];
        [webView displayRectIgnoringOpacity:offscreenRect inContext:bitmapContext];
        [NSGraphicsContext restoreGraphicsState];
    
        // Create a small + large thumbs
        NSImage *smallThumbImage = [[NSImage alloc] initWithSize:thumbSizeSmall];  
        NSImage *largeThumbImage = [[NSImage alloc] initWithSize:thumbSizeLarge];
    
        [smallThumbImage lockFocus];
        [[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];  
        [offscreenRep drawInRect:CGRectMake(0, 0, thumbSizeSmall.width, thumbSizeSmall.height)];  
        NSBitmapImageRep *smallThumbOutput = [[NSBitmapImageRep alloc] initWithFocusedViewRect:CGRectMake(0, 0, thumbSizeSmall.width, thumbSizeSmall.height)];  
        [smallThumbImage unlockFocus];  
    
        [largeThumbImage lockFocus];  
        [[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];  
        [offscreenRep drawInRect:CGRectMake(0, 0, thumbSizeLarge.width, thumbSizeLarge.height)];  
        NSBitmapImageRep *largeThumbOutput = [[NSBitmapImageRep alloc] initWithFocusedViewRect:CGRectMake(0, 0, thumbSizeLarge.width, thumbSizeLarge.height)];  
        [largeThumbImage unlockFocus];  
    
        // Write out small
        NSString *writePathSmall = [issueProvider.imageDestinationPath stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@-collection-%03d-page-%03d_small.png", locale, collectionIndex, pageIndex]];
        NSData *dataSmall = [smallThumbOutput representationUsingType:NSPNGFileType properties: nil];
        [dataSmall writeToFile:writePathSmall atomically: NO];
    
        // Write out lage
        NSString *writePathLarge = [issueProvider.imageDestinationPath stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@-collection-%03d-page-%03d_large.png", locale, collectionIndex, pageIndex]];
        NSData *dataLarge = [largeThumbOutput representationUsingType:NSPNGFileType properties: nil];
        [dataLarge writeToFile:writePathLarge atomically: NO];
    }];
    

    Hope this helps!

提交回复
热议问题