sgr-ksmt / PDFGenerator

A simple generator of PDF written in Swift.
MIT License
755 stars 110 forks source link

Vector text only if view is in hierarchy #119

Open cameron-erdogan opened 4 years ago

cameron-erdogan commented 4 years ago

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?

adam-konecny commented 4 years ago

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

cameron-erdogan commented 4 years ago

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

vaghasiya commented 2 years ago

any update ?

zyflovelam commented 2 years ago

Same question, any update on this issue?

adam-konecny commented 2 years ago

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.