I'm trying to generate a pdf with a transparent background for vector graphics and I've tried everything google has suggested, but I still keep getting a white background every time.
Filling the page with clear color didn't seem to work, and I couldn't figure out how to set the background of the page.
Any thoughts?
func draw() -> Data {
let metadata = []
let format = UIGraphicsPDFRendererFormat()
format.documentInfo = metadata as [String: Any]
let pageWidth = 3 * 72.0
let pageHeight = 3 * 72.0
let pageRect = CGRect(x: 0, y: 0, width: pageWidth, height: pageHeight)
let renderer = UIGraphicsPDFRenderer(bounds: pageRect, format: format)
let data = renderer.pdfData { (context) in
context.beginPage()
let currentContext = UIGraphicsGetCurrentContext()
currentContext?.setFillColor(UIColor.clear.cgColor)
currentContext?.fill(pageRect)
let circlePath = UIBezierPath(arcCenter: CGPoint(x: pageWidth / 2, y: pageHeight / 2), radius: CGFloat(100), startAngle: CGFloat(0), endAngle: CGFloat(Double.pi * 2), clockwise: true)
UIColor.init(cgColor: colors[0].cgColor).setFill();
UIColor.init(cgColor: colors[0].cgColor).setStroke();
circlePath.fill()
circlePath.stroke()
}
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
do {
try data.write(to: URL(fileURLWithPath: "\(documentsPath)/file.pdf"))
} catch {
print(error)
}
return data
}