Programmatically Generate PDF from HTML on iPhone

前端 未结 6 1386
挽巷
挽巷 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:27

    I haven't tried this myself so i have nothing to offer concrete but I'd have to imagine there has to be an easy way to do this on iPhone due to the imaging model. I'd look deeper into the documentation.

    As to pushing back with the client that is up to you but there are probably multiple reasons for wanting to keep everything local. Frankly I would not be pleased at all to here from somebody I hired that he couldn't manage this particular task. So think long and hard about this push back. Oh even if you do push back a webserver is a poor choice. I'd go back a step further and investgate why you need something in HTML in the first place.

    0 讨论(0)
  • 2021-02-10 16:32

    To generate a pdf from an HTML, you need to render the html into a web view, and take snapshots of the web view, and render them into an image context. The tutorial might be helpful:

    http://www.ioslearner.com/convert-html-uiwebview-pdf-iphone-ipad/

    0 讨论(0)
  • 2021-02-10 16:32

    Why not use a WebService, send the HTML page to this and retrieve the PDF-file ?

    That way you can use iTextSharp and C#, and you're done in about 2 minutes.
    Plus (if you're evil) you can store and see all the data on your server.

    0 讨论(0)
  • 2021-02-10 16:36

    I made it by following this SO: https://stackoverflow.com/a/13342906/448717

    In order to maintain the same content's proportions I had to multiply the size of the WKWebView 1.25 times the printableRect's size set for the UIPrinterRenderer, as the screen points differs from the PostScript's... I guess.

    0 讨论(0)
  • 2021-02-10 16:37

    I've written a little piece of code that takes an NSAttributedString from DTCoreText, and renders it into a paged PDF file. You can find it on my GitHub Repository. It won't render images or complex html, but it should serve for most uses. Plus, if you're familiar with CoreText, you can extend my PDF frame setter to generate these items.

    So what it does now: Give it an HTML string, and it will use DTCoreText to generate an NSAttributedString, then render that into a PDF. It hands back the location that it saved the PDF file in the app's Documents folder.

    0 讨论(0)
  • 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];
        ...
    }
    
    0 讨论(0)
提交回复
热议问题