예제 #1
0
def _create_compressed_file_object(source):
    """
    Create a file like object as ``/EmbeddedFile`` compressing it with deflate.

    :return:
        the object representing the compressed file stream object
    """
    md5 = hashlib.md5()
    compress = zlib.compressobj()

    pdf_file_object = PdfDict(Type=PdfName('EmbeddedFile'),
                              Filter=PdfName('FlateDecode'))

    # pdfrw needs Latin-1-decoded unicode strings in object.stream
    pdf_file_object.stream = ''
    size = 0
    for data in iter(lambda: source.read(4096), b''):
        size += len(data)
        md5.update(data)
        pdf_file_object.stream += compress.compress(data).decode('latin-1')
    pdf_file_object.stream += compress.flush(zlib.Z_FINISH).decode('latin-1')
    pdf_file_object.Params = PdfDict(CheckSum=PdfString('<{}>'.format(
        md5.hexdigest())),
                                     Size=size)
    return pdf_file_object
예제 #2
0
def write_fillable_pdf(input_pdf_path, output_pdf_path, data_dict,
                       camposCheckBox):

    template_pdf = PdfReader(input_pdf_path)
    #Necesario para que se vean cambios
    template_pdf.Root.AcroForm.update(
        PdfDict(NeedAppearances=PdfObject('true')))

    #Por cada pagina del PDF
    for page in template_pdf.pages:
        annotations = page[ANNOT_KEY]

        #Para cada anotacion de la pagina
        for annotation in annotations:
            if annotation[SUBTYPE_KEY] == WIDGET_SUBTYPE_KEY:
                if annotation[ANNOT_FIELD_KEY]:
                    key = annotation[ANNOT_FIELD_KEY][1:-1]
                    if key in data_dict.keys():

                        #HACK PARA LOS CHECK. Si es true, se marcan, sino no
                        if key in camposCheckBox:
                            if (data_dict[key] == 'true'):
                                annotation.update(
                                    PdfDict(V='{}'.format(data_dict[key]),
                                            AS=PdfName('Yes')))
                            #Si no se pone nada, por defecto no se marcan
                            continue

                    #Objeto necesario para que al rellenar se vean los campos
                        rct = annotation.Rect
                        hight = round(float(rct[3]) - float(rct[1]), 2)
                        width = (round(float(rct[2]) - float(rct[0]), 2))

                        xobj = PdfDict(
                            BBox=[0, 0, width, hight],
                            FormType=1,
                            Resources=PdfDict(
                                ProcSet=[PdfName.PDF, PdfName.Text]),
                            Subtype=PdfName.Form,
                            Type=PdfName.XObject)
                        #assign a stream to it
                        xobj.stream = '''/Tx BMC
                        BT
                        /Helvetica 8.0 Tf
                        1.0 5.0 Td
                        0 g
                        (''' + data_dict[key] + ''') Tj
                        ET EMC'''

                        #Actualizamos la anotacion en el PDF
                        annotation.update(
                            PdfDict(AP=PdfDict(N=xobj),
                                    V='{}'.format(data_dict[key])))

    #Escribimos el PDF ya anotado al PATH de salida
    PdfWriter().write(output_pdf_path, template_pdf)
예제 #3
0
def write_fillable_pdf(input_pdf_path, output_pdf_path, data_dict):

    template_pdf = PdfReader(input_pdf_path)
    # Para que se vean los campos rellenados
    template_pdf.Root.AcroForm.update(
        PdfDict(NeedAppearances=PdfObject('true')))
    for page in template_pdf.pages:
        annotations = page[ANNOT_KEY]

        for annotation in annotations:
            if annotation[SUBTYPE_KEY] == WIDGET_SUBTYPE_KEY:
                if annotation[ANNOT_FIELD_KEY]:
                    key = annotation[ANNOT_FIELD_KEY][1:-1]
                    if key in data_dict.keys():

                        #HACK PARA LOS CHECK - Guardar documento a mano con los checks que sean. Y aquí si es un check, evitarlo y no cambiar
                        if key == "untitled6" or key == "untitled21" or key == "untitled22" or key == "untitled23" or key == "untitled24" or key == "untitled25":

                            continue

                    #this depends on page orientation
                        rct = annotation.Rect
                        hight = round(float(rct[3]) - float(rct[1]), 2)
                        width = (round(float(rct[2]) - float(rct[0]), 2))

                        xobj = PdfDict(
                            BBox=[0, 0, width, hight],
                            FormType=1,
                            Resources=PdfDict(
                                ProcSet=[PdfName.PDF, PdfName.Text]),
                            Subtype=PdfName.Form,
                            Type=PdfName.XObject)
                        #assign a stream to it
                        xobj.stream = '''/Tx BMC
                        BT
                        /Helvetica 8.0 Tf
                        1.0 5.0 Td
                        0 g
                        (''' + data_dict[key] + ''') Tj
                        ET EMC'''
                        annotation.update(
                            PdfDict(AP=PdfDict(N=xobj),
                                    V='{}'.format(data_dict[key])))
                        #annotation.update(pdfrw.PdfDict(V='{}'.format(data_dict[key]),AP='{}'.format(data_dict[key])))

    PdfWriter().write(output_pdf_path, template_pdf)