How to export a SwiftUI View to PDF (Vector)

Hi,

I have a SwiftUI View which contains Images and Borders.
I'd like to export this to a Vector-PDF.

I tried
Code Block
let pdfExportView = NSHostingView(rootView: MySwiftUIView())
let pdfData = pdfExportView.dataWithPDF(inside: pdfExportView.bounds)


But the PDF does not contain the Images and the borders. Is there any other solution? PDF as Bitmap is not an option.
Someone will provide a more knowledgeable answer, but it looks like only text comes across right now. GeometryReader/ResolvedShapeView/etc don't emit anything, maybe because they're implemented GPU-side when possible (like a fill with masking, CoreAnimation-style).
If you want to always be able to access accurate PDF representations, try CoreGraphics.
I also tried CoreGraphics with drawing the layer in the CGContext … without any luck. Result was only a white PDF.
Also I tried to use printing but the result was same as
Code Block
dataWithPDF()
… Text and BezierPath had been drawn, Borders and Images not.


Used on of my DTS I got the reply to file a bug … which I did :)

But I also found a workaround: While CoreGraphics was not working first, I found out that a PDF with all SwiftUI-Elements is produced if the CoreGraphics-part is wrapped in DispatchQueue.main.async
The result is flipped and mirrored but that shouldn't be a problem.
And yes, I included the workaround in the sample project which I attached to the bug report.
Would it be possible to post the code of your workaround -- or at least a skeleton of it? I have been struggling with the same issue and using CoreGraphics isn't working for me yet but I'm not sure I'm using CGContext correct with PDF files. Would be extremely helpful. Thanks.
How to export a SwiftUI View to PDF (Vector)
 
 
Q