Skip to content

Instantly share code, notes, and snippets.

@itswenb
Last active November 24, 2021 09:58
Show Gist options
  • Save itswenb/24a1e7d283a11488cf49fe3788ebb54f to your computer and use it in GitHub Desktop.
Save itswenb/24a1e7d283a11488cf49fe3788ebb54f to your computer and use it in GitHub Desktop.
Objective-C version of Convert HTML to Image
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGI
#define A4PaperWidth 595.2
#define A4PaperHeight 842.
@interface UIImage (Extension)
+ (UIImage *)imageWithHTMLString:(NSString *)htmlString imageWidth:(CGFloat)imageWidth;
- (UIImage *)appendImageAtEnd:(UIImage *)image;
@end
NS_ASSUME_NONNULL_END
#import "UIImage+Extension.h"
@implementation UIImage (Extension)
+ (UIImage *)imageWithHTMLString:(NSString *)htmlString imageWidth:(CGFloat)imageWidth {
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES).firstObject stringByAppendingPathComponent:@"tmp.pdf"];
UIPrintPageRenderer *renderer = [[UIPrintPageRenderer alloc] init];
UIMarkupTextPrintFormatter *formatter = [[UIMarkupTextPrintFormatter alloc] initWithMarkupText:htmlString];
[renderer addPrintFormatter:formatter startingAtPageAtIndex:0];
formatter.maximumContentWidth = imageWidth;
formatter.maximumContentHeight = CGFLOAT_MAX;
CGRect printableRect = CGRectMake(0, 0, imageWidth, CGFLOAT_MAX);
[renderer setValue:[NSValue valueWithCGRect:printableRect] forKey:@"paperRect"];
[renderer setValue:[NSValue valueWithCGRect:printableRect] forKey:@"printableRect"];
CGFloat totalHeight = 0.f;
for (int i = 0; i < formatter.pageCount; i ++) {
CGRect rect = [formatter rectForPageAtIndex:i];
totalHeight += rect.size.height;
}
UIGraphicsBeginPDFContextToFile(path, CGRectMake(0, 0, imageWidth, totalHeight), nil);
UIGraphicsBeginPDFPage();
[renderer drawPageAtIndex:0 inRect:CGRectMake(0, 0, imageWidth, totalHeight)];
UIGraphicsEndPDFContext();
// Read PDF Ref and get Images
CGPDFDocumentRef ref = CGPDFDocumentCreateWithURL((CFURLRef)([NSURL fileURLWithPath:path]));
UIGraphicsImageRendererFormat *format = [[UIGraphicsImageRendererFormat alloc] init];
CGPDFPageRef page = CGPDFDocumentGetPage(ref, 1);
CGRect box = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
if (@available(iOS 12.0, *)) {
format.preferredRange = YES;
} else {
format.prefersExtendedRange = YES;
}
UIGraphicsImageRenderer *graphicsRenderer = [[UIGraphicsImageRenderer alloc] initWithSize:CGSizeMake(imageWidth, totalHeight) format:format];
UIImage *result = [graphicsRenderer imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull ctx) {
[UIColor.whiteColor set];
[ctx fillRect:box];
CGContextTranslateCTM(ctx.CGContext, 0.f, box.size.height);
CGContextScaleCTM(ctx.CGContext, 1.f, -1.f);
CGContextDrawPDFPage(ctx.CGContext, page);
}];
if ([NSFileManager.defaultManager fileExistsAtPath:path]) {
NSError *error;
[NSFileManager.defaultManager removeItemAtPath:path error:&error];
if (error) {
NSLog(@"Delete file error: %@", error);
}
}
return result;
}
- (UIImage *)appendImageAtEnd:(UIImage *)image {
UIGraphicsBeginImageContextWithOptions(CGSizeMake(MAX(self.size.width, image.size.width), self.size.height + image.size.height), YES, self.scale);
[self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)];
[image drawInRect:CGRectMake(0, self.size.height, image.size.width, image.size.height)];
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultImage;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment