Search code examples
pythonpython-3.xreportlab

Python reportlab, the problem of not positioning the side title at the beginning of the PDF page it is on


#-----PDF SECTION----------------------------------------------------------------------------
from reportlab.lib.pagesizes import letter, mm
from reportlab.lib.units import inch
from reportlab.pdfgen import canvas
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib import colors
from reportlab.platypus import Paragraph, SimpleDocTemplate, Spacer, Image, PageBreak
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont

if "roboto-bold" or "roboto-regular" not in [fam.lower() for fam in pdfmetrics.getRegisteredFontNames()]:
    pdfmetrics.registerFont(TTFont("Roboto-Bold", "fonts\Roboto\Roboto-Bold.ttf"))
    pdfmetrics.registerFont(TTFont("Roboto-Regular", "fonts\Roboto\Roboto-Regular.ttf"))
else:
    print("Roboto-Bold/Roboto-Regular fonts have already been registered.")


styles = getSampleStyleSheet()

# footer style
footer_style = ParagraphStyle(
    name='footer',
    parent=styles['Normal'],
    fontName='Helvetica',
    fontSize=8,  # Yazı boyutunu küçülttük
    textColor=colors.lightgrey,
    alignment=0,  # Sola hizalama için değeri 0 olarak ayarladık
)

# footer text
footer_text = 'Created by HepsiBurada Price Tracker App'
footer = Paragraph(footer_text, footer_style)

# header style
header_style = ParagraphStyle(
    name='header',
    parent=styles['Normal'],
    fontName='Helvetica-Oblique',
    fontSize=10,
    textColor=colors.black,
    alignment=1,
)

# cover page style
cover_style = ParagraphStyle(
    name='cover',
    parent=styles['Normal'],
    fontName='Roboto-Bold',
    textColor=colors.black,
    alignment=1
)


# side header style

side_header_style = ParagraphStyle(
    name='side_header',
    parent=styles['Normal'],
    fontName='Roboto-Bold',
    fontSize=18,
    textColor=colors.black,
    leftIndent=-63, 
    topPadding=0,
    spaceBefore=-20,
    spaceAfter=15 
)



# result style
result_style = ParagraphStyle(
    name='result',
    parent=styles['Normal'],
    fontName='Roboto-Regular',
    fontSize=12,
    textColor=colors.black,
    leftIndent=-63,
    spaceAfter=14
   
)




def generate_pdf(pdf_file, content, footer_style, header_style, cover_style, result_style, side_header_style):
   
    doc = SimpleDocTemplate(pdf_file, pagesize=(215.9 * mm, 279.4 * mm))

   


    # cover page
    cover_text = '<font size="30" color="black">Pınar Süt 1lt Analysis Results</font> <br/><br/><br/><br/><br/><br/>'

    cover = Paragraph(cover_text, cover_style)

    # İstediğiniz görseli ekleyin
    image = Image('cover_template_for_pdf/cover_template.jpg', 7 * inch, 6.5 * inch)

    # page content
    content = [cover, Spacer(1, 0.5 * inch), image, PageBreak()] + content 



    def page_number(canvas, doc):
        page_num = canvas.getPageNumber()
        canvas.setFillColorRGB(0.2, 0.2, 0.2)
        text = "{}".format(page_num)
        canvas.setFont("Helvetica", 10)  # Sayfa numarası yazı boyutu
        canvas.drawCentredString(doc.width / 2 + doc.leftMargin, 0.5 * inch, text)
        
        
        canvas.setFont("Helvetica", 8)  # Alt bilgi yazı boyutu
        canvas.setFillColorRGB(191/255, 87/255, 0)  # Turuncu rengi ayarla
        footer_width = canvas.stringWidth(footer_text, "Helvetica", 8)
        x = doc.leftMargin - 20 * mm  # Sola yasla ve sol taraftan 10 mm boşluk bırak
        canvas.drawString(x, 0.3 * inch, footer_text)  

        
    
        
    def onFirstPage(canvas, doc):
        canvas.saveState()
        page_number(canvas, doc)
        # gray line
        canvas.setStrokeColor(colors.orange)
        canvas.setLineWidth(1)
        canvas.rect(0, 0.75 * inch, doc.pagesize[0], 1, fill=True, stroke=False)
        canvas.restoreState()

    def onLaterPages(canvas, doc):
        canvas.saveState()
        page_number(canvas, doc)
        # gray line
        canvas.setStrokeColor(colors.orange)
        canvas.setLineWidth(1)
        canvas.rect(0, 0.75 * inch, doc.pagesize[0], 1, fill=True, stroke=False)
        canvas.restoreState()

    
     
        
    doc.build(content, onFirstPage=onFirstPage, onLaterPages=onLaterPages)
    return pdf_file



subheading1="1. Summary information"
Searched_product=f"Searched product: {search_term}"
Seller_offering_the_lowest_price_for_the_searched_product=f"Seller offering the lowest price for the searched product: {product['seller']}"
Link_of_the_searched_product=f"Link of the searched product: {product['url'][0]}"
Duration_of_tracking_the_product=f"Duration of tracking the product:{tracking_days}"

subheading2="2. Graphics"    
    
# Add the content to the existing PDF
content_list  = [Paragraph(subheading1, side_header_style), 
Paragraph(Searched_product,result_style),
Paragraph(Seller_offering_the_lowest_price_for_the_searched_product,result_style),
Paragraph(Link_of_the_searched_product,result_style),
Paragraph(Duration_of_tracking_the_product,result_style),
PageBreak(),
Paragraph(subheading2, side_header_style)]
           
#Image(lowest_highest_prices_filename, width=500, height=250)
   
generate_pdf
("ornek.pdf",content_list,footer_style,header_style,cover_style,result_style,side_header_style)

Hello friends, I cannot move the side headers ( Summary information and Graphics) to the top of the page. Actually, I use two parameters like topPadding=0, spaceBefore=-20 in the side header style, but it doesn't work. A thick gray line appears at the junction of the two pages. I thought the problem might be with it, but I couldn't eliminate it either. How Can I fix this?

The Problem:

enter image description here


Solution

  •  doc = SimpleDocTemplate(pdf_file, pagesize=(215.9 * mm, 279.4 * mm), topMargin=17)  ########### 
    

    enter image description here