Skip to content

Instantly share code, notes, and snippets.

@nyg
Last active May 9, 2025 20:44
Show Gist options
  • Save nyg/b8cd742250826cb1471f to your computer and use it in GitHub Desktop.
Save nyg/b8cd742250826cb1471f to your computer and use it in GitHub Desktop.
iOS, Swift: Create a PDF file from an HTML string.
// Thanks to http://www.labs.saachitech.com/2012/10/23/pdf-generation-using-uiprintpagerenderer
// Note: including images in the HTML won't work, see here:
// https://github.com/nyg/HTMLWithImagesToPDF
import UIKit
// 1. Create a print formatter
let html = "<b>Hello <i>World!</i></b>"
let fmt = UIMarkupTextPrintFormatter(markupText: html)
// 2. Assign print formatter to UIPrintPageRenderer
let render = UIPrintPageRenderer()
render.addPrintFormatter(fmt, startingAtPageAt: 0)
// 3. Assign paperRect and printableRect
let page = CGRect(x: 0, y: 0, width: 595.2, height: 841.8) // A4, 72 dpi
render.setValue(page, forKey: "paperRect")
render.setValue(page, forKey: "printableRect")
// 4. Create PDF context and draw
let pdfData = NSMutableData()
UIGraphicsBeginPDFContextToData(pdfData, .zero, nil)
for i in 0..<render.numberOfPages {
UIGraphicsBeginPDFPage();
render.drawPage(at: i, in: UIGraphicsGetPDFContextBounds())
}
UIGraphicsEndPDFContext();
// 5. Save PDF file
guard let outputURL = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent("output").appendingPathExtension("pdf")
else { fatalError("Destination URL not created") }
pdfData.write(to: outputURL, atomically: true)
print("open \(outputURL.path)") // command to open the generated file
@KaanYT
Copy link

KaanYT commented Aug 12, 2020

Objective-C Version

+(NSData*)htmlToPdf:(NSString*)html{
    //Formatter
    UIMarkupTextPrintFormatter * fmt = [[UIMarkupTextPrintFormatter alloc] initWithMarkupText:html];
    
    //Renderer
    UIPrintPageRenderer * render = [[UIPrintPageRenderer alloc] init];
    [render addPrintFormatter:fmt startingAtPageAtIndex:0];
    
    //Assign Printable Rect
    CGRect pageSize = CGRectMake(0, 0, 595.2, 841.8);// A4, 72 dpi
    [render setValue:[NSValue valueWithCGRect:pageSize] forKey:@"paperRect"];
    [render setValue:[NSValue valueWithCGRect:pageSize] forKey:@"printableRect"];
    
    //Create PDF Context and Draw
    NSMutableData* data = [[NSMutableData alloc] init];
    UIGraphicsBeginPDFContextToData(data, CGRectZero, NULL);
    
    for (int i=0; i<render.numberOfPages; i++) {
        UIGraphicsBeginPDFPage();
        [render drawPageAtIndex:i inRect: UIGraphicsGetPDFContextBounds()];
    }

    UIGraphicsEndPDFContext();

    return data;
}

@matrosovDev
Copy link

I also faced with image issue, just wonder if this resolved I've tried a few solutions but it still does not work for me. I tried to save image to the files and used base 64 image, but still with no luck

https://stackoverflow.com/questions/66251424/creating-pdf-file-with-uiimage-inserted-to-html-code

@Udit-sys
Copy link

Hey,
I think you need to use javascript with your HTML code to load image in web view and then create a pdf
Use evaluateJavaScript() to run javascript in web view

@matrosovDev
Copy link

Hey,
I think you need to use javascript with your HTML code to load image in web view and then create a pdf
Use evaluateJavaScript() to run javascript in web view

Now all works good! I used link above https://github.com/nyg/HTMLWithImagesToPDF in a wrong way. Image is in the PDF file now. thanks

@startover205
Copy link

It should be UIGraphicsBeginPDFContextToData(pdfData, page, nil) instead of UIGraphicsBeginPDFContextToData(pdfData, .zero, nil) to have the correct PDF page size and prevent content from being cut off.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment