sgr-ksmt / PDFGenerator

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

Bug? Irregularities with UIScrollview #40

Closed ChaosSaber closed 8 years ago

ChaosSaber commented 8 years ago

Hello,

So I'm currently experimenting a bit with your PDFgenerator and found some strange things happening, but maybe i'm just using it wrong. What i experienced is that the content size of the scroll view, is the size of the generated pdf, but the frame size of the view is the visible part of the pdf. so when i have a frame size of 595:842 and a content size of 595:1684 i have a page of two A4 sizes but only half of that is written, even if there would be text on the second half.

Thats what i have done: I have a view in a storyboard. in that view is a UIScrollview and in that scrollview is a stack view with labels and images. i created the pdf like this:

    private func GetUIView() -> UIView{
        var result = UIView()
        let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewControllerWithIdentifier("PDFOutput") as! PDFOutputViewController
        vc.view.frame.size = CGSize(width: 595, height: 842)
        // fill view Controller with Data
        for view in vc.view.subviews{
            if view is UIScrollView{
                (view as! UIScrollView).contentSize = CGSize(width: 595, height: 1684)
                result = view as! UIScrollView
            }
        }
        return result
    }
    let dst = NSHomeDirectory().stringByAppendingString("/test.pdf")
    PDFGenerator.generate(GetUIView(), outputPath: dst)

The output is an half empty page. When i change the frame-height to 1684 i get an full page.

On a side note a little request. Make the PDFPageSizes public so we users can use them too, because the standard accessor is only internal. And now at last a question. Is it possible to split these long UIScrollview, so that they occupy exactly one page. Would also be a nice standard feature. Something like that:

    func SplitScrollView(scrollView: UIScrollView) -> [UIView]{
        var views = [UIView]()
        for i in 0 ... Int(scrollView.contentSize.height / PDFPageSize.A4.height){
            scrollView.contentOffset = CGPoint(x: 0, y: Int(a4.height * CGFloat(i))
            scrollView.bounds.size = PDFPageSize.A4
            views.append(scrollview.extractVisiblePart())
        }
    }

Thanks in Advance

ChaosSaber commented 8 years ago

I Just found this blog about the slicing of an UIScrollView: http://blog.mosheberman.com/generating-a-pdf-from-a-uiscrollview/ Maybe it helps you to implement this.

sgr-ksmt commented 8 years ago

@Chaosprogrammierer Hi. Sorry for the late reply..

Admittedly, this library does not support the feature to divide the desired size.

I will try implementing this feature based on your advice(blog).
please wait for a while :bow:

ChaosSaber commented 8 years ago

ok Nevermind that made the cut right through a line of text. so maybe not that ideal method. without adjusting the bounds a bit that it don't cut a line in two.

And what about the first part? that the visible part of the pdf is bound to the frame size. is that intended?

sgr-ksmt commented 8 years ago

@Chaosprogrammierer

And what about the first part? that the visible part of the pdf is bound to the frame size. is that intended?

also this issue will fix...! please wait.

ChaosSaber commented 8 years ago

OK thanks, now i just need to find out the actual height of my stack view.

ChaosSaber commented 8 years ago

After using layoutSubviews() on the stack view, scrollview and primary view it created a complete pdf page. So problem 1 was my fault because i was using it wrong. it probably didn't render the non visible views. This can be closed now.

sgr-ksmt commented 8 years ago

I fixed this problem. If a view has super view , renderInContext does not work properly. So I fixed like this:

let superView: UIVIew? = view.superView // store super view if a view has it
view.removeFromSuperView() // remove temporary
// ... render process
superView?.addSubView(view) // restore

see detail : #44

Also fix access control of PDFPageSize : #43

These revisions are released as v1.4.1 .


and.. I will try implementing #41

Thanks reporting problem 🙇

raudabaugh commented 8 years ago

The 1.4.1 update broke PDF generation for me. I am using this library with the latest version of the Charts library (https://github.com/danielgindi/Charts). Generating a PDF from a LineChartView removes it from the superview but never restores it.

sgr-ksmt commented 8 years ago

@raudabaugh Sorry for replying late.. I resolved this issue at #46 .

Please update to v1.4.2 ! 🙇

vermasagar49 commented 5 years ago

extractVisiblePart() what will be the implementation of this method