移至ReportLab中的下一帧

时间:2019-05-17 10:36:05

标签: python reportlab platypus

在ReportLab中,我有一个由2个垂直框架组成的页面模板。我要在这里实现的是-在页面上(第一帧)添加一些动态文本之后,我想转到第二帧的顶部。

我试图通过计算第一帧中文本对象的高度,然后插入一个高度等于(doc.height-第一帧中文本对象的权重)的空格来实现这一点。但是,这不起作用。这是简化的代码及其输出。

             #include<iostream>
             #include<string.h>
             #include<vector>
             #include<algorithm>
             using namespace std;


             class Customer;
             class flipkart
             {
                vector<Customer*>list;
                vector<Customer*>::iterator it;
                public:
                void Register(Customer *customer)
                {
                   list.push_back(customer);
                }
                void unregister(Customer *customer)
                {
                    list.erase(remove(list.begin(), list.end(),customer), list.end()); 
                }

                void notify(string item,float vprice);
             };


             class observer
             {
                 public:
                 virtual void update(string item,float vprice)=0;
             };
             class Customer:public observer
             {
                 string name;
                 public:
                 Customer(string n)
                 {
                     name=n;
                 }

                 void update(string item,float vprice)
                 {
                     cout<<"**Flipkart**updated price for "<<item<<" is:"<<vprice<<" Rupees only, request recieved by "<<name<<endl;
                 }
             };

             void flipkart::notify(string item,float vprice)
             {
                 for(it=list.begin();it!=list.end();it++)
                 {
                     (*it)->update(item,vprice);
                 }
             }

             class product:public flipkart
             {
                 public:
                 void change_price(string item,float vprice)
                 {
                     notify(item,vprice);
                 }
             };

             int main()
             {
                 Customer customer1("Dhoni"),customer2("Yuvraj"),customer3("Kohli");
                 product LCD;

                 LCD.Register(&customer1);
                 LCD.Register(&customer2);
                 LCD.Register(&customer3);

                 LCD.change_price("LCD HD2 TV",12000);

                 LCD.unregister(&customer2);
                 cout<<"after unregisterng customer2:\n";

                 LCD.change_price("LCD HD2 TV",11500);
             }

enter image description here

有人知道我在这里做错了吗?谢谢!

1 个答案:

答案 0 :(得分:1)

我建议您尝试使用FrameBreak而不是Spacer,因为我认为休息是解决您问题的更好方法。

以下是代码的修改版本,以显示结果:

import random
from reportlab.lib.pagesizes import A4, landscape
from reportlab.lib.styles import ParagraphStyle
from reportlab.lib.units import inch
from reportlab.platypus import (
    BaseDocTemplate, PageTemplate, PageBreak, Frame, FrameBreak, Spacer, Paragraph,
)


if __name__ == "__main__":
    style_1 = ParagraphStyle(
        name='Stylo',
        fontName='Helvetica',
        fontSize=10,
        leading=12)
    doc = BaseDocTemplate(
        'test_spacer.pdf',
        showBoundary=1,
        pagesize=landscape(A4),
        topMargin=1*inch,
        bottomMargin=1*inch,
        leftMargin=1*inch,
        rightMargin=1*inch)

    frameCount = 2
    frameWidth = doc.width / frameCount
    frameHeight = doc.height - 0.05*inch

    frame_list = [
        Frame(
            x1=doc.leftMargin,
            y1=doc.bottomMargin,
            width=frameWidth,
            height=frameHeight),
        Frame(
            x1=doc.leftMargin + frameWidth,
            y1=doc.bottomMargin,
            width=frameWidth,
            height=frameHeight),
    ]
    doc.addPageTemplates([PageTemplate(id='frames', frames=frame_list), ])

    story = []
    for i, x in enumerate(['A', 'B', 'C']):
        # add text in first frame
        for _ in range(3):
            story.append(
                Paragraph(
                    x * random.randint(50, 100),
                    style_1))

        # jump to next frame
        story.append(FrameBreak())

        # add text in second frame
        story.append(
            Paragraph(
                'This should be on the top of the 2nd Frame! ' + x,
                style_1))

        story.append(PageBreak())

    doc.build(story)

它提供此输出(此图像仅显示前2页):

enter image description here