Exemplo n.º 1
0
def pdFview(filename, invoice):
    """
        cette views est cree pour la generation du PDF
    """

    if not filename:
        filename = get_temp_filename('pdf')
        print(filename)
    #on recupere les items de la facture
    items_invoice = InvoiceItem.filter(invoices=invoice)

    # Static source pdf to be overlayed
    PDFSOURCE = 'fact_source.pdf'
    TMP_FILE = 'tmp.pdf'
    DATE_FORMAT = u"%d/%m/%Y"

    DEFAULT_FONT_SIZE = 11
    FONT = 'Courier-Bold'
    # A simple function to return a leading 0 on any single digit int.

    def double_zero(value):
        try:
            return '%02d' % value
        except TypeError:
            return value

    # setup the empty canvas
    from io import FileIO as file
    # from Common.pyPdf import PdfFileWriter, PdfFileReader
    from PyPDF2 import PdfFileWriter, PdfFileReader

    # PDF en entrée
    input1 = PdfFileReader(file(PDFSOURCE, "rb"))

    # PDF en sortie
    output = PdfFileWriter()
    # Récupération du nombre de pages
    n_pages = input1.getNumPages()
    # Pour chaque page
    for i in range(n_pages):
        # Récupération de la page du doc initial (input1)
        page = input1.getPage(i)

        p = canvas.Canvas(TMP_FILE, pagesize=A4)
        # p.setFont(FONT, DEFAULT_FONT_SIZE)

        p.drawString(130, 683, str(invoice.number))
        p.drawString(98, 667, (invoice.client))

        p.drawString(460, 683, str(invoice.date.strftime(DATE_FORMAT)))

        # On ecrit les invoiceitem
        x, y = 40, 610
        for i in items_invoice:
            p.drawString(x, y, str(i.quantity).rjust(11, ' '))
            p.drawString(x + 75, y, str(i.description))
            p.drawString(x + 340, y, str(i.price).rjust(10, ' '))
            p.drawString(x + 430, y, str(i.price * i.quantity).rjust(10, ' '))
            y -= 20
        # on teste le type
        if invoice.type_ == "Facture":
            p.drawString(59, 80, "Pour acquit: ")
        else:
            p.drawString(59, 80, "Pour acceptation: ")

        p.drawString(435, 80, "Le fournisseur: ")
        #On calcul le montant total hors taxe et sa conversion en lettre
        ht = 0
        for i in items_invoice:
            montant = i.price * i.quantity
            ht += montant

        ht_en_lettre = cel(ht)

        p.drawString(470, 150, str(ht).rjust(10, ' '))
        ht_en_lettre1, ht_en_lettre2 = controle_caratere(ht_en_lettre +
                                                        " FCFA", 46, 40)
        p.drawString(258, 116, (ht_en_lettre  + " FCFA"))
        # p.drawString(258, 116, (ht_en_lettre1))
        # p.drawString(53, 100, (ht_en_lettre2))

        # legal_infos, legal_infos1 = controle_caratere(Config.BP, 55, 55)
        # p.drawString(90, 14, legal_infos)
        # p.drawString(90, 6, legal_infos1)
        p.showPage()
        # Sauvegarde de la page
        p.save()
        # Création du watermark
        watermark = PdfFileReader(file(TMP_FILE, "rb"))
        # Création page_initiale+watermark
        page.mergePage(watermark.getPage(0))
        # Création de la nouvelle page
        output.addPage(page)
    # Nouveau pdf
    file_dest = filename + ".pdf"
    outputStream = file(file_dest, u"wb")
    output.write(outputStream)
    outputStream.close()

    return file_dest
Exemplo n.º 2
0
def pdf_view(filename, invoice):
    """
        cette views est cree pour la generation du PDF
    """

    if not filename:
        filename = get_temp_filename('pdf')
        # print(filename)
    # on recupere les items de la facture
    items_invoice = Report.filter(invoice=invoice)
    # Static source pdf to be overlayed
    PDFSOURCE = Config.INV_TEMPLATE_PDF
    TMP_FILE = os.path.join(Config.ROOT_DIR, 'tmp.pdf')
    DATE_FORMAT = u"%d/%m/%Y"
    DEFAULT_FONT_SIZE = 11
    FONT = 'Times-Roman'

    # PDF en entrée
    input1 = PdfFileReader(file(PDFSOURCE, "rb"))
    # PDF en sortie
    output = PdfFileWriter()
    # Récupération du nombre de pages
    n_pages = input1.getNumPages()
    # Pour chaque page

    for i in range(n_pages):
        # Récupération de la page du doc initial (input1)
        page = input1.getPage(i)
        p = canvas.Canvas(TMP_FILE, pagesize=A4)
        p.setFont(FONT, DEFAULT_FONT_SIZE)
        p.drawString(115, 639, str(invoice.number))
        p.drawString(82, 625, (invoice.client.name))
        p.drawString(445, 625, str(invoice.date.strftime(DATE_FORMAT)))
        # On ecrit les invoiceitem
        x, y = 122, 574
        x_qty = x
        x_description = x + 10
        x_price = x + 340
        x_amount = x + 433

        for i in items_invoice:
            p.drawRightString(x_qty, y, str(i.qty))
            p.drawString(x_description, y, str(i.product.name))
            p.drawRightString(x_price, y, str(
                formatted_number(i.selling_price)))
            p.drawRightString(x_amount, y, str(formatted_number(
                i.selling_price * i.qty)))
            y -= 17
        # On calcul le montant total hors taxe et sa conversion en lettre
        ht = sum([(val.selling_price * val.qty) for val in items_invoice])
        tax_rate = invoice.tax_rate if invoice.tax else 0
        mt_tax = int((ht * tax_rate) / 100)
        htt = mt_tax + ht
        ht_en_lettre = num2words(ht, lang="fr")
        ht_en_lettre1, ht_en_lettre2 = controle_caratere(
            ht_en_lettre + " francs CFA", 50, 50)
        p.drawString(260, 191, (ht_en_lettre1))
        p.drawString(50, 175, (ht_en_lettre2))
        p.setFont('Times-Bold', 11)
        p.drawString(52, 639, "Facture N° :")
        p.drawString(x_price - 20, 248, str(tax_rate) + "%")
        # TVA
        p.drawRightString(x_amount, 248, device_amount(mt_tax))
        # Hors Taxe
        p.drawRightString(x_amount, 265, str(device_amount(ht)))
        # Tout Taxe
        p.drawRightString(x_amount, 232, str(device_amount(htt)))
        x_foot = 145
        p.drawString(50, x_foot, "Acceptation" if invoice.type_ ==
                     "Proforma" else "Acquit")
        p.drawString(490, x_foot, "Fournisseur")
        p.showPage()
        # Sauvegarde de la page
        p.save()
        # Création du watermark
        watermark = PdfFileReader(file(TMP_FILE, "rb"))
        # Création page_initiale+watermark
        page.mergePage(watermark.getPage(0))
        # Création de la nouvelle page
        output.addPage(page)
    # Nouveau pdf
    file_dest = filename + ".pdf"
    try:
        outputStream = file(file_dest, u"wb")
        output.write(outputStream)
        outputStream.close()
        return file_dest
    except OSError as e:
        from Common.ui.util import raise_error
        raise_error(u"Impossible de lancer le PDF", """
                    Car un autre en cours d'utilistation. Kill le \n{}""".format(e))
        return
Exemplo n.º 3
0
def pdf_view(filename, order):
    """
        cette views est cree pour la generation du PDF
    """

    if not filename:
        filename = get_temp_filename('pdf')
    # on recupere la facture a afficher
    invoice = order
    # on recupere les items de la facture
    items_invoice = Report.filter(invoice=invoice)

    # Static source pdf to be overlayed
    PDF_SOURCE = 'tools/fact_source.pdf'
    TMP_FILE = 'tools/tmp.pdf'
    DATE_FORMAT = u"%d/%m/%Y"

    DEFAULT_FONT_SIZE = 10
    FONT = 'Courier-Bold'
    # A simple function to return a leading 0 on any single digit int.

    def double_zero(value):
        try:
            return '%02d' % value
        except TypeError:
            return value

    # setup the empty canvas

    from pyPdf import PdfFileWriter, PdfFileReader
    # PDF en entrée
    input1 = PdfFileReader(file(PDF_SOURCE, "rb"))
    # PDF en sortie
    output = PdfFileWriter()
    # Récupération du nombre de pages
    n_pages = input1.getNumPages()
    # Pour chaque page
    for i in range(n_pages):
        # Récupération de la page du doc initial (input1)
        page = input1.getPage(i)

        # p = canvas.Canvas(buffer, pagesize=A4)
        p = canvas.Canvas(TMP_FILE, pagesize=A4)
        p.setFont(FONT, DEFAULT_FONT_SIZE)

        # Création de l'objet PDF, en utilisant l'objet de réponse comme "fichier"
        # on afffiche l'image de l'orgamisation
        try:
            p.drawImage(u'%s' % Config.APP_LOGO, 60, 740)
        except:
            print(u"logo non disponible!")
            pass

        p.drawString(370, 735, unicode(Config.NAME_ORGA))
        # On trace Une ligne horizontale
        p.line(60, 730, 535, 730)

        p.drawString(59, 25, Config.NAME_ORGA + " - tel : " +
                     str(Config.TEL_ORGA) + " - " + unicode(Config.ADRESS_ORGA))

        legal_infos, legal_infos1 = controle_caratere(Config.BP, 55, 55)

        p.drawString(90, 14, legal_infos)
        p.drawString(90, 6, legal_infos1)
        p.drawString(
            60, 706, str(invoice.type_) + " N°: " + str(invoice.number))
        p.drawString(370, 706, "Date: " + str(invoice.date.strftime(DATE_FORMAT)) +
                     " à " + str(invoice.location))
        p.drawString(60, 690, "Doit: " + (invoice.client))

        if invoice.subject:
            p.drawString(60, 664, "Objet: " + str(invoice.subject))

        # On ecrit les invoiceitem
        x, y = 40, 600
        for i in items_invoice:
            p.drawString(x, y, str(i.qty).rjust(10, ' '))
            p.drawString(x + 75, y, (i.product.__str__()))
            p.drawString(x + 340, y, str(i.selling_price).rjust(10, ' '))
            p.drawString(
                x + 435, y, str(i.selling_price * i.qty).rjust(10, ' '))
            y -= 20
        # on teste le type
        if invoice.type_ == "Facture":
            p.drawString(59, 95, "Pour acquit: ")
        else:
            p.drawString(59, 95, "Pour acceptation: ")

        p.drawString(435, 95, "Le Providers: ")
        # On calcul le montant total hors taxe et sa conversion en lettre
        ht = 0
        for i in items_invoice:
            montant = i.selling_price * i.qty
            ht += montant
        p.drawString(476, 204, str(ht).rjust(10, ' '))
        ht_en_lettre = cel(ht)
        # Calcul du TTC avec le TVA s'il existe
        if invoice.tax:
            TVA = (invoice.tax_rate * ht) / 100
            p.drawString(476, 183.5, str(TVA).rjust(10, ' '))
            TTC = ht + TVA
            p.drawString(476, 164, str(TTC).rjust(10, ' '))
            ht_en_lettre = cel(TTC)
            ht_en_lettre1, ht_en_lettre2 = controle_caratere(
                ht_en_lettre + " FCFA", 46, 40)
            p.drawString(263.8, 133, (ht_en_lettre1))
            p.drawString(53, 120, (ht_en_lettre2))

            p.drawString(415, 183.5, str(invoice.tax_rate))

        else:
            TTC = ht
            p.drawString(476, 164, str(TTC).rjust(10, ' '))
            ht_en_lettre1, ht_en_lettre2 = controle_caratere(
                ht_en_lettre + " FCFA", 46, 40)
            p.drawString(263.8, 133, (ht_en_lettre1))
            p.drawString(53, 120, (ht_en_lettre2))
            p.drawString(415, 183.5, str(0))
            p.drawString(476, 183.5, str(0).rjust(10, ' '))
        p.showPage()
        # Sauvegarde de la page
        p.save()
        # Création du watermark
        watermark = PdfFileReader(file(TMP_FILE, "rb"))
        # Création page_initiale+watermark
        page.mergePage(watermark.getPage(0))
        # Création de la nouvelle page
        output.addPage(page)
    # Nouveau pdf
    file_dest = filename + u".pdf"
    outputStream = file(file_dest, u"wb")
    output.write(outputStream)
    outputStream.close()

    return file_dest
Exemplo n.º 4
0
def pdf_maker(filename, dmd):
    """
        cette views est cree pour la generation du PDF
    """

    if not filename:
        filename = get_temp_filename('pdf')

    # Static source pdf to be overlayed
    pd_source = 'immat_source.pdf'
    tmp_file = 'tmp.pdf'
    # DATE_FORMAT = u"%d/%m/%Y"

    default_font_size = 11
    font = 'Courier'

    # A simple function to return a leading 0 on any single digit int.

    def double_zero(value):
        try:
            return '%02d' % value
        except TypeError:
            return value

    # setup the empty canvas
    from io import FileIO as file
    # from Common.pyPdf import PdfFileWriter, PdfFileReader
    from PyPDF2 import PdfFileWriter, PdfFileReader

    # PDF en entrée
    input1 = PdfFileReader(file(pd_source, "rb"))

    # PDF en sortie
    output = PdfFileWriter()
    # Récupération du nombre de pages
    n_pages = input1.getNumPages()
    # Pour chaque page
    immat = Immatriculation.select().where(
        Immatriculation.scoop == dmd.scoop).get()
    for i in range(n_pages):
        # Récupération de la page du doc initial (input1)
        page = input1.getPage(i)
        y = 630
        x = 70
        p = canvas.Canvas(tmp_file, pagesize=A4)
        p.setFont(font, default_font_size)
        p.drawString(x + 70, y + 73, str(dmd.scoop.display_region()))
        p.drawString(x + 70, y + 37, str(dmd.scoop.display_cercle()))

        denom1, denom2 = controle_caratere(dmd.scoop.denomination, 60, 65)
        p.drawString(x, y - 90, denom1)
        p.drawString(x, y - 120, denom2)

        p.drawString(x + 120, y - 154, str(dmd.id))
        p.drawString(x + 300, y - 154,
                     str(dmd.start_date.strftime("%d / %B / %Y")))
        p.drawString(x + 70, y - 190, str(immat.name_declarant))
        p.drawString(x + 65, y - 224, str(immat.display_quality()))
        p.drawString(x + 176, y - 262, str(immat.procuration))
        p.drawString(x + 120, y - 298, str(dmd.scoop.display_commune()))
        p.drawString(x + 355, y - 298, str(dmd.scoop.display_vfq()))
        p.drawString(x + 90, y - 334, str(dmd.scoop.rue))
        p.drawString(x + 317, y - 334, str(dmd.scoop.porte))
        p.drawString(x + 67, y - 369, str(dmd.scoop.tel))
        p.drawString(x + 227, y - 369, str(dmd.scoop.bp))
        p.drawString(x + 350, y - 369, str(dmd.scoop.email))
        p.drawString(x + 100, y - 400, str(dmd.scoop.immatricule))
        p.drawString(x + 60, y - 470, str(dmd.scoop.display_cercle()))
        p.drawString(x + 370, y - 470, str(immat.date.strftime("%d/%m/%Y")))
        p.showPage()
        # Sauvegarde de la page
        p.save()
        # Création du watermark
        watermark = PdfFileReader(file(tmp_file, "rb"))
        # Création page_initiale+watermark
        page.mergePage(watermark.getPage(0))
        # Création de la nouvelle page
        output.addPage(page)
    # Nouveau pdf
    file_dest = filename + ".pdf"
    outputStream = file(file_dest, u"wb")
    output.write(outputStream)
    outputStream.close()

    return file_dest