Convert PDF to Uiimage

Convert PDF data to UIImage and download into album in Swift iOS

This code is working fine for my question:

    GHServiceManager.shared.getPDF(fileName: self.pdfName, success: { (ssdata) in

let pdfData = ssdata as CFData
let provider:CGDataProvider = CGDataProvider(data: pdfData)!
let pdfDoc:CGPDFDocument = CGPDFDocument(provider)!
let pdfPage:CGPDFPage = pdfDoc.page(at: 1)!
var pageRect:CGRect = pdfPage.getBoxRect(.mediaBox)
pageRect.size = CGSize(width:pageRect.size.width, height:pageRect.size.height)

print("\(pageRect.width) by \(pageRect.height)")

UIGraphicsBeginImageContext(pageRect.size)
let context:CGContext = UIGraphicsGetCurrentContext()!
context.saveGState()
context.translateBy(x: 0.0, y: pageRect.size.height)
context.scaleBy(x: 1.0, y: -1.0)
context.concatenate(pdfPage.getDrawingTransform(.mediaBox, rect: pageRect, rotate: 0, preserveAspectRatio: true))
context.drawPDFPage(pdfPage)
context.restoreGState()
let pdfImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()

dukePhotoLibrary?.saveImage(image: pdfImage)
})

Load PDF into UIImage or UIImageView?

PDFs can be loaded by UIWebView. If you need more control you can use Quartz 2D to render PDFs: Quartz2D

Swift UIImage convert as PDF

create pdf file data as:

func createPDFDataFromImage(image: UIImage) -> NSMutableData {
let pdfData = NSMutableData()
let imgView = UIImageView.init(image: image)
let imageRect = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)
UIGraphicsBeginPDFContextToData(pdfData, imageRect, nil)
UIGraphicsBeginPDFPage()
let context = UIGraphicsGetCurrentContext()
imgView.layer.render(in: context!)
UIGraphicsEndPDFContext()

//try saving in doc dir to confirm:
let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last
let path = dir?.appendingPathComponent("file.pdf")

do {
try pdfData.write(to: path!, options: NSData.WritingOptions.atomic)
} catch {
print("error catched")
}

return pdfData
}

Sample Image

High quality UIImage from PDF

When you ask the PDF for it's page size, you're getting a width/height for 72 PPI. You might try creating a context that's scaled up using a scale transform. For example, if you wanted to render at 300 dpi, add a scale transform to scale by 300.0/72.0.

If you export as TIFF, you will be able to encapsulate the final PPI (300) of the generated image.

Converting PDF to Image - Swift iOS

Make sure that your pptURL is file url.

URL(string: "path/to/pdf") and URL(fileURLWithPath: "path/to/pdf") are different things and you must use the last one while initiating your url.

The output should start with "file:///" prefix, f.e.

file:///Users/dev/Library/Developer/CoreSimulator/Devices/4FF18699-D82F-4308-88D6-44E3C11C955A/data/Containers/Bundle/Application/8F230041-AC15-45D9-863F-5778B565B12F/myApp.app/example.pdf

Convert PDF to UIImageView

What are you doing with the CGContextTranslateCTM(context, 0.0, 320); call?

You should extract the proper metrics form the pdf, with code like this:

 cropBox = CGPDFPageGetBoxRect(page, kCGPDFCropBox);
rotate = CGPDFPageGetRotationAngle(page);

Also, as you see, the pdf might has rotation info, so you need to use the CGContextTranslateCTM/CGContextRotateCTM/CGContextScaleCTM depending on the angle.

You also might wanna clip any content that is outside of the CropBox area, as pdf has various viewPorts that you usually don't wanna display (e.g. for printers so that seamless printing is possible) -> use CGContextClip.

Next, you're forgetting that the pdf reference defines a white background color. There are a lot of documents out there that don't define any background color at all - you'll get weird results if you don't draw a white background on your own --> CGContextSetRGBFillColor & CGContextFillRect.

Converting PDF to UIImage, without CoreGraphic's PDF Classes

The binary data within a PDF file cannot be translated directly into an UIImage type. You need to find a third party library or some other way of converting a PDF file into an UIImage.



Related Topics



Leave a reply



Submit