|
I am using Paul's method to extract a custom View to PDF. https://www.hackingwithswift.com/quick-start/swiftui/how-to-render-a-swiftui-view-to-a-pdf My custom View is dynamic (I am using a LazyVGrid) and although the width is fixed to (let's say to A4 page size) the height grows as per the content. Does anyone know how to add pages into pdf as per the View height? Is something that PDFKit could address? I am trying to find a guide on how to use but I am getting confused. So far I mastered Paul's method but the limitation is when the View exceeds the A4 size height... Thanks! |
|
Can a simple option add a fixed height to a fixed width by simply setting the parameter for the A4 format: height = width * √2. Or does LazyVGrid not support fixed height? |
|
Hi Sergey, the LazyVGrid grows as the contents needs, thus limiting the height it won't help. Using Paul's method I can create a perfect A4 page pdf, but if my content is bigger the extra one is not plotted in the A4.. The challenge is somehow to tell SWIFT that when the height of the content exceeds 842(A4 height in points), to introduce more pages. Using pdf.beginPDFPage and pdf.endPDFPage I can add pages in the pdf but they are just copies of the first one. |
|
Oops, then I don’t know, I misunderstood the question and thought that you need all the content to be on one page. |
|
As I understand there is no easy way to make several pages with image renderer as it basically creates image and then places in pdf. It seems like that if you use image renderer you will need to calculate how many grid items you want per page and then put some logic in code how to calculate that. THE CODE UPDATED!
I have put sample data in the file so it is clear what is what. The only issue that views are drawn from x: 0, y: 0 from the left bottom side. Could not find the way to make it go from top to bottom instead of bottom up... Here how it looks like when you try to print: https://ibb.co/zshHPsT |
|
I'm so glad I found this post, as I've been struggling with this too. I have rebuilt my LazyVGrid to now correctly handle 1 Grid per loop, but when I try to add the ShareLink to my View - nothing happens. What am I doing wrong? My Current code is available on Git - https://github.com/TheApApp/cardtracker/blob/main/SwiftUI/ViewEventsView.swift - Ah! I figured it out, now to just reformat the page to correctly print all the content :). Thanks again for this Forum Thread! |
TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.