Programmatically Generate PDF from HTML on iPhone

前端 未结 6 1385
挽巷
挽巷 2021-02-10 15:57

I am looking for a way to programmatically (in obj-c) generate a PDF file from a local html file. I am dynamically generating the html from user inputs, I need to create the PD

6条回答
  •  隐瞒了意图╮
    2021-02-10 16:39

    I've never tried this so I have no idea if it'll work, but how about loading the HTML into a UIWebView, and then make the view draw itself into a PDF context? E.g.

    UIWebView *webview = [[UIWebView alloc] initWithFrame:CGRectMake(...)];
    [webview loadHTMLString:html baseURL:...];
    

    Then:

    - (void)webViewDidFinishLoad:(UIWebView *)webview {
        CGPDFContextRef pdfContext = CGPDFContextCreateWithURL(...);
        [webview.layer drawInContext:pdfContext];
        ...
    }
    

提交回复
热议问题