Here's the sample code used to create PDF data and then save it to the documents directory: Here the "self" is nothing but UIView
self.isOpaque = false
self.backgroundColor = UIColor.clear
let pdfPageFrame = self.bounds
let pdfData = NSMutableData()
UIGraphicsBeginPDFContextToData(pdfData, pdfPageFrame, nil)
UIGraphicsBeginPDFPageWithInfo(pdfPageFrame, nil)
let pdfContext = UIGraphicsGetCurrentContext()!
pdfContext.setFillColor(UIColor.clear.cgColor)
pdfContext.setAlpha(0)
self.layer.opacity = 0
self.layer.render(in: pdfContext)
UIGraphicsEndPDFContext()
return pdfData
When I save the PDFData, it result's in white background.
I want to use this pdf in PDFkit, not in image view or button. Two issues:
- How to generate a pdf with transparent background.
- How to ensure this transparency stays when I use it in pdfkit.