Get a PDF/PNG as output from a UIWebView or UIView

后端 未结 4 2151
傲寒
傲寒 2020-12-02 12:22

Is there any way to get the content of a UIWebView and convert it to a PDF or PNG file? I\'d like to get similar output to that available on the Mac by selectin

4条回答
  •  遥遥无期
    2020-12-02 13:23

    You can use the following category on UIView to create a PDF file:

    #import 
    
    @implementation UIView(PDFWritingAdditions)
    
    - (void)renderInPDFFile:(NSString*)path
    {
        CGRect mediaBox = self.bounds;
        CGContextRef ctx = CGPDFContextCreateWithURL((CFURLRef)[NSURL fileURLWithPath:path], &mediaBox, NULL);
    
        CGPDFContextBeginPage(ctx, NULL);
        CGContextScaleCTM(ctx, 1, -1);
        CGContextTranslateCTM(ctx, 0, -mediaBox.size.height);
        [self.layer renderInContext:ctx];
        CGPDFContextEndPage(ctx);
        CFRelease(ctx);
    }
    
    @end
    

    Bad news: UIWebView does not create nice shapes and text in the PDF, but renders itself as an image into the PDF.

提交回复
热议问题