I am using a UIPrintPageRenderer
together with a UIMarkupTextPrintFormatter
to print an html page which is rendered in a UIWebView
. I am trying to achieve the following:
I want to render the html content as a transparent layer onto the context but the background color of the html content is always white when rendered with the UIPrintPageRenderer
in the context covering up all the content below.
The html style contains the following code snippet:
body {
background-color: transparent !important;;
-webkit-print-color-adjust: exact;
}
In the UIWebView
I can see that the background of the html is indeed transparent because I set the background of the UIWebView
to a different color.
This is the code I use for creating the PDF data:
let printPageRenderer = UIPrintPageRenderer()
let printFormatter = UIMarkupTextPrintFormatter(markupText: htmlContent)
printPageRenderer.addPrintFormatter(printFormatter, startingAtPageAt: 0)
let data = NSMutableData()
UIGraphicsBeginPDFContextToData(data, Constants.Pdf.rect, nil)
let context = UIGraphicsGetCurrentContext()!
for i in 0..<printPageRenderer.numberOfPages {
UIGraphicsBeginPDFPage()
let backCoverView = UIView(frame: Constants.Pdf.rect)
backCoverView.backgroundColor = UIColor(patternImage: UIImage(named: "background.png")!)
backCoverView.layer.render(in: context)
context.clear(printableRect)
printPageRenderer.setValue(NSValue(cgRect: printableRect), forKey: "printableRect")
printPageRenderer.drawPage(at: i, in: UIGraphicsGetPDFContextBounds())
}
UIGraphicsEndPDFContext()
My problem is that the html content which is rendered over the "background.png" is not transparent but has a white background. The transparent attribute in the html style seems to be ignored by the UIPrintPageRenderer
. Setting the background-color
in the html style to e.g. blue
changes the rendered html background color from white to blue. I also tried making the rendered html transparent by making the context transparent with context.clear(printableRect)
to no avail.
How can I make UIPrintPageRenderer
print the html content with a transparent background so the already rendered background in the context is not covered by a white background of the html?