PDFGenerator icon indicating copy to clipboard operation
PDFGenerator copied to clipboard

Vector text only if view is in hierarchy

Open cameron-erdogan opened this issue 5 years ago • 5 comments

I'm observing some weird behavior when generating PDFs. My goal is to get a PDF from a UIView with UILabels where the PDF has vectorized, searchable text, not a rasterization of text.

When I pass in a view in the hierarchy and visible on screen to PDFGenerator.generated(by: ), the PDF is rendered with vectorized text, but the view I passed in gets mangled as a result. If instead I create a new instance of that view and pass that in instead, the resulting PDF is completely rasterized.

Any thoughts?

cameron-erdogan avatar Dec 19 '19 22:12 cameron-erdogan

@cameron-erdogan I have the same issue, did you find a workaround?

adam-konecny avatar Jun 04 '20 19:06 adam-konecny

@racak94 sadly no. I ended up just using the pdf with rasterized text.

cameron-erdogan avatar Jun 04 '20 21:06 cameron-erdogan

any update ?

vaghasiya avatar Oct 08 '21 12:10 vaghasiya

Same question, any update on this issue?

zyflovelam avatar Feb 25 '22 14:02 zyflovelam

I found a solution. I replaced every UILabel used in the PDF with my custom subclass that overrides draw(_:in:) like this:

override func draw(_ layer: CALayer, in ctx: CGContext) {
        let isPDF = !UIGraphicsGetPDFContextBounds().isEmpty

        if !self.layer.shouldRasterize && isPDF {
            self.draw(self.bounds)
        } else {
            super.draw(layer, in: ctx)
        }
}

Here you can find the Stack Overflow thread.

adam-konecny avatar Jul 11 '22 16:07 adam-konecny