def update_form_values(infile, outfile, newvals=None):
    pdf = PdfFileReader(open(infile, 'rb'), strict=False)
    if "/AcroForm" in pdf.trailer["/Root"]:
        pdf.trailer["/Root"]["/AcroForm"].update(
            {NameObject("/NeedAppearances"): BooleanObject(True)})
    else:
        print("pdf.trailer[/Root] does not exist")

    writer = PdfFileWriter()
    writer.cloneReaderDocumentRoot(pdf)
    set_need_appearances_writer(writer)
    if "/AcroForm" in writer._root_object:
        writer._root_object["/AcroForm"].update(
            {NameObject("/NeedAppearances"): BooleanObject(True)})
    else:
        print("/Acroform not in _root_object")

    for i in range(pdf.getNumPages()):
        page = pdf.getPage(i)
        try:
            if newvals:
                writer.updatePageFormFieldValues(page, newvals)
            # writer.addPage(page)
        except Exception as e:
            print(repr(e))
            # writer.addPage(page)
    
    with open(outfile, 'wb') as out:
        writer.write(out)
        writer.write(out)
Example #2
0
def add_annot_to_page(annot, page, output):
    annot_ref = output._addObject(annot)

    if "/Annots" in page:
        page[NameObject("/Annots")].append(annot_ref)
    else:
        page[NameObject("/Annots")] = ArrayObject([annot_ref])
Example #3
0
def test_add_named_destination():
    reader = PdfReader(os.path.join(RESOURCE_ROOT, "pdflatex-outline.pdf"))
    writer = PdfWriter()

    for page in reader.pages:
        writer.add_page(page)

    from PyPDF2.generic import NameObject

    writer.add_named_destination(NameObject("A named dest"), 2)
    writer.add_named_destination(NameObject("A named dest2"), 2)

    from PyPDF2.generic import IndirectObject

    assert writer.get_named_dest_root() == [
        "A named dest",
        IndirectObject(7, 0, writer),
        "A named dest2",
        IndirectObject(10, 0, writer),
    ]

    # write "output" to PyPDF2-output.pdf
    tmp_filename = "dont_commit_named_destination.pdf"
    with open(tmp_filename, "wb") as output_stream:
        writer.write(output_stream)

    # Cleanup
    os.remove(tmp_filename)
Example #4
0
def generatePdf(infile, outfile):
    pdf = PdfFileReader(open(infile, "rb"), strict=False)
    if "/AcroForm" in pdf.trailer["/Root"]:
        pdf.trailer["/Root"]["/AcroForm"].update(
            {NameObject("/NeedAppearances"): BooleanObject(True)})

    pdf2 = PdfFileWriter()
    updateFormProperlyWriter(pdf2)
    if "/AcroForm" in pdf2._root_object:
        pdf2._root_object["/AcroForm"].update(
            {NameObject("/NeedAppearances"): BooleanObject(True)})

    # Add pages
    for x in range(pdf.getNumPages() - 1):
        pdf2.addPage(pdf.getPage(x))
        pdf2.updatePageFormFieldValues(pdf2.getPage(x), FORM_VALUES)

    # Flatten form fields.
    flat_page = pdf2.getPage(0)
    for j in range(0, len(flat_page['/Annots'])):
        writer_annot = flat_page['/Annots'][j].getObject()
        for field in FLATTEN_VALUES:
            if writer_annot.get('/T') == field:
                writer_annot.update({
                    NameObject("/Ff"): NumberObject(1)  # make ReadOnly
                })

    outputStream = open(outfile, "wb")
    pdf2.write(outputStream)
Example #5
0
    def download_pdf(self):
        pdf_writer = PyPDF2.PdfFileWriter()
        self.env['comunicazione.liquidazione'].set_need_appearances_writer(
            pdf_writer)
        if "/AcroForm" in pdf_writer._root_object:
            # Acro form is form field, set needs appearances to fix printing issues
            pdf_writer._root_object["/AcroForm"].update(
                {NameObject("/NeedAppearances"): BooleanObject(True)})

        for file in self.pdf_list:
            pdf_reader = PyPDF2.PdfFileReader(io.BytesIO(
                base64.decodebytes(file.datas)),
                                              strict=False)

            if "/AcroForm" in pdf_reader.trailer["/Root"]:
                pdf_reader.trailer["/Root"]["/AcroForm"].update(
                    {NameObject("/NeedAppearances"): BooleanObject(True)})

            pageCount = pdf_reader.getNumPages()
            for iPage in range(0, pageCount):
                pdf_writer.addPage(pdf_reader.getPage(iPage))

        output_stream = BytesIO()
        pdf_writer.write(output_stream)
        self.file_pdf_export = base64.encodestring(output_stream.getvalue())
def meta(input_pdf, output_pdf, value):

    pdf_writer = PdfFileWriter()
    pdf_reader = PdfFileReader(input_pdf)

    for page in range(pdf_reader.getNumPages()):
        pdf_writer.addPage(pdf_reader.getPage(page))

    # pdf_writer.encrypt(user_pwd=password, owner_pwd=None,
    #                    use_128bit=True)

    infoDict = pdf_writer._info.getObject()

    infoDict.update({NameObject('/Version'): createStringObject(u'234ds2')})
    info = pdf_reader.documentInfo
    for key in info:
        infoDict.update({NameObject(key): createStringObject(info[key])})

    # add the grade
    # infoDict.update({NameObject('/Grade'): createStringObject(u'A+')})
    # infoDict.update({NameObject('/Grade2'): createStringObject(u'A+')})
    infoDict.update({NameObject('/Key'): createStringObject(value)})

    with open(output_pdf, 'wb') as fh:
        pdf_writer.write(fh)
Example #7
0
    def process(self, inputFile1):
        input1 = PyPDF2.PdfFileReader(inputFile1)
        output = PyPDF2.PdfFileWriter()
        # TODO: Do not access protected classes
        output._info.getObject().update(input1.documentInfo)

        # Add the outlines, if any
        if "/Outlines" in input1.trailer["/Root"]:
            if output._root:
                # Backwards-compatible with PyPDF2 version 1.21
                output._root.getObject()[NameObject("/Outlines")] = (
                    output._addObject(input1.trailer["/Root"]["/Outlines"]))
            else:
                # Compatible with PyPDF2 version 1.22+
                output._root_object[NameObject("/Outlines")] = (
                    output._addObject(input1.trailer["/Root"]["/Outlines"]))

        for (num, page) in enumerate(input1.pages):
            if num in self.operations:
                for mergeFile, mergeNumber in self.operations[num]:
                    merger = PyPDF2.PdfFileReader(mergeFile)
                    mergerPage = merger.getPage(mergeNumber)
                    mergerPage.mergePage(page)
                    page = mergerPage
            output.addPage(page)

        outputFile = six.BytesIO()
        output.write(outputFile)
        return outputFile
Example #8
0
def set_need_appearances_writer(writer):
    """
    Helper para escribir el pdf
    :param writer: el pdf
    :return:
    """
    # See 12.7.2 and 7.7.2 for more information:
    # http://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/PDF32000_2008.pdf
    try:
        catalog = writer._root_object
        # get the AcroForm tree and add "/NeedAppearances attribute
        if "/AcroForm" not in catalog:
            writer._root_object.update({
                NameObject("/AcroForm"):
                IndirectObject(len(writer._objects), 0, writer)
            })

        need_appearances = NameObject("/NeedAppearances")
        writer._root_object["/AcroForm"][need_appearances] = BooleanObject(
            True)
        return writer

    except Exception as e:
        print('set_need_appearances_writer() catch : ', repr(e))
        return writer
Example #9
0
def addHighlightToPage(highlight, page, output):
    """Add a highlight to a page.

    Args:
        highlight (DictionaryObject) : Highlight information.
        page (PageObject)            : A single page within a PDF file.
        output (PdfFileWriter)       : A pdf writer.

    Examples:
        >>> from gummy.utils import createHighlight, addHighlightToPage
        >>> from PyPDF2 import PdfFileWriter, PdfFileReader
        >>> page_no = 0
        >>> pdfOutput = PdfFileWriter()
        >>> with open("input.pdf", mode="rb") as inPdf:
        ...     pdfInput = PdfFileReader(inPdf)
        ...     page = pdfInput.getPage(page_no)
        ...     highlight = createHighlight(bbox=(10,10,90,90), contents="COMMENT", color=(1,1,0))
        ...     addHighlightToPage(highlight, page, pdfOutput)
        ...     pdfOutput.addPage(page)
        ...     with open("output.pdf", mode="wb") as outPdf:
        ...         pdfOutput.write(outPdf)
    """
    from PyPDF2.generic import (NameObject, ArrayObject)
    highlight_ref = output._addObject(highlight)
    if "/Annots" in page:
        page[NameObject("/Annots")].append(highlight_ref)
    else:
        page[NameObject("/Annots")] = ArrayObject([highlight_ref])
Example #10
0
def createAnnotPdf(geom_type, myShapePdf):
    # input variables
    # part 1: read geometry pdf to get the vertices and rectangle to use
    source = PdfFileReader(open(myShapePdf, 'rb'))
    geomPage = source.getPage(0)
    mystr = geomPage.getObject()['/Contents'].getData()
    # to pinpoint the string part: 1.19997 791.75999 m 1.19997 0.19466 l 611.98627 0.19466 l 611.98627 791.75999 l 1.19997 791.75999 l
    # the format seems to follow x1 y1 m x2 y2 l x3 y3 l x4 y4 l x5 y5 l
    geomString = mystr.split('S\r\n')[0].split('M\r\n')[1]
    coordsString = [
        value for value in geomString.split(' ')
        if value not in ['m', 'l', '']
    ]

    # part 2: update geometry in the map
    if geom_type.upper() == 'POLYGON':
        pdf_geom = PdfFileReader(open(annot_poly, 'rb'))
    elif geom_type.upper() == 'POLYLINE':
        pdf_geom = PdfFileReader(open(annot_line, 'rb'))
    page_geom = pdf_geom.getPage(0)

    annot = page_geom['/Annots'][0]
    updateVertices = "annot.getObject().update({NameObject('/Vertices'):ArrayObject([FloatObject(" + coordsString[
        0] + ")"
    for item in coordsString[1:]:
        updateVertices = updateVertices + ',FloatObject(' + item + ')'
    updateVertices = updateVertices + "])})"
    exec(updateVertices)

    xcoords = []
    ycoords = []
    for i in range(0, len(coordsString) - 1):
        if i % 2 == 0:
            xcoords.append(float(coordsString[i]))
        else:
            ycoords.append(float(coordsString[i]))

    # below rect seems to be geom bounding box coordinates: xmin, ymin, xmax,ymax
    annot.getObject().update({
        NameObject('/Rect'):
        ArrayObject([
            FloatObject(min(xcoords)),
            FloatObject(min(ycoords)),
            FloatObject(max(xcoords)),
            FloatObject(max(ycoords))
        ])
    })
    annot.getObject().pop('/AP')  # this is to get rid of the ghost shape

    annot.getObject().update({NameObject('/T'): createStringObject(u'ERIS')})

    output = PdfFileWriter()
    output.addPage(page_geom)
    annotPdf = os.path.join(scratch, "annot.pdf")
    outputStream = open(annotPdf, "wb")
    #output.setPageMode('/UseOutlines')
    output.write(outputStream)
    outputStream.close()
    output = None
    return annotPdf
Example #11
0
    def __init__(self, path=None):
        """[summary]
        
        Arguments:
            template_id {[type]} -- [description]
        
        Keyword Arguments:
            path {[type]} -- [description] (default: {None})
        """

        self.template_id = '2746-sd_2589'
        if path is None:
            self.path = app.root_path + '/data/cerfa/'
        else:
            self.path = path

        self.inputStream = open(self.path + self.template_id + '.pdf', "rb")
        self.outputStream = open(UPLOAD_FOLDER + '/cerfa.pdf', "wb")

        self.pdf_reader = PdfFileReader(self.inputStream, strict=False)
        if "/AcroForm" in self.pdf_reader.trailer["/Root"]:
            self.pdf_reader.trailer["/Root"]["/AcroForm"].update(
                {NameObject("/NeedAppearances"): BooleanObject(True)})

        self.pdf_writer = PdfFileWriter()
        self.set_need_appearances_writer(self.pdf_writer)
        if "/AcroForm" in self.pdf_writer._root_object:
            self.pdf_writer._root_object["/AcroForm"].update(
                {NameObject("/NeedAppearances"): BooleanObject(True)})
Example #12
0
    def createHighlight(self,x1, y1, x2, y2, meta, color = [1, 0, 0]):
        newHighlight = DictionaryObject()

        newHighlight.update({
            NameObject("/F"): NumberObject(4),
            NameObject("/Type"): NameObject("/Annot"),
            NameObject("/Subtype"): NameObject("/Highlight"),

            NameObject("/T"): TextStringObject(meta["author"]),
            NameObject("/Contents"): TextStringObject(meta["contents"]),

            NameObject("/C"): ArrayObject([FloatObject(c) for c in color]),
            NameObject("/Rect"): ArrayObject([
                FloatObject(x1),
                FloatObject(y1),
                FloatObject(x2),
                FloatObject(y2)
            ]),
            NameObject("/QuadPoints"): ArrayObject([
                FloatObject(x1),
                FloatObject(y2),
                FloatObject(x2),
                FloatObject(y2),
                FloatObject(x1),
                FloatObject(y1),
                FloatObject(x2),
                FloatObject(y1)
            ]),
        })

        return newHighlight
Example #13
0
 def _update_page_form_checkbox_values(self, page, fields):
     """Updates the checkbox values in a form. 
     It is needed in order the checked answers to become visible.
     
     Parameters:
         page (PyPDF2.pdf.PageObject): Page object from a PDF file.
         fields (dict): Dictionary containing the key -> value assignments.
     
     (From/inspired by: https://github.com/mstamy2/PyPDF2/issues/355)
     """
     for j in range(0, len(page['/Annots'])):
         writer_annot = page['/Annots'][j].getObject()
         for field in fields:
             if writer_annot.get('/T') == field:
                 if fields[field] in self.YES:
                     writer_annot.update({
                         NameObject("/V"):
                         NameObject(fields[field]),
                         NameObject("/AS"):
                         NameObject(fields[field])
                     })
                 else:
                     writer_annot.update({
                         NameObject("/V"):
                         TextStringObject(fields[field])
                     })
Example #14
0
    def addHighlightToPage(self,highlight, page, output):
        highlight_ref = output._addObject(highlight)

        if "/Annots" in page:
            page[NameObject("/Annots")].append(highlight_ref)
        else:
            page[NameObject("/Annots")] = ArrayObject([highlight_ref])
Example #15
0
def updateCheckboxValues(page, fields):
    for j in range(0, len(page['/Annots'])):
        writer_annot = page['/Annots'][j].getObject()
        for field in fields:
            if writer_annot.get('/T') == field:
                writer_annot.update({NameObject("/V"): NameObject(fields[field]),
                                     NameObject("/AS"): NameObject(fields[field])})
Example #16
0
def write_fillable_pdf(file, output_pdf_path, data_dict):
    out_dir = os.path.join(BASE_DIR,
                           "tmp/" + str(random.randrange(20, 200, 3)) + ".pdf")
    INVOICE_TEMPLATE_PATH = os.path.join(BASE_DIR, file)
    input_stream = open(INVOICE_TEMPLATE_PATH, "rb")
    pdf_reader = PyPDF2.PdfFileReader(input_stream, strict=False)
    if "/AcroForm" in pdf_reader.trailer["/Root"]:
        pdf_reader.trailer["/Root"]["/AcroForm"].update(
            {NameObject("/NeedAppearances"): BooleanObject(True)})
    pdf_writer = PyPDF2.PdfFileWriter()
    set_need_appearances_writer(pdf_writer)
    if "/AcroForm" in pdf_writer._root_object:
        # Acro form is form field, set needs appearances to fix printing issues
        pdf_writer._root_object["/AcroForm"].update(
            {NameObject("/NeedAppearances"): BooleanObject(True)})
    pdf_writer.addPage(pdf_reader.getPage(0))
    page = pdf_writer.getPage(0)
    pdf_writer.updatePageFormFieldValues(page, data_dict)
    for j in range(0, len(page['/Annots'])):
        writer_annot = page['/Annots'][j].getObject()
        for field in data_dict:
            # -----------------------------------------------------BOOYAH!
            if writer_annot.get('/T') == field:
                writer_annot.update({NameObject("/Ff"): NumberObject(1)})
            # -----------------------------------------------------
    output_stream = BytesIO()
    pdf_writer.write(output_stream)
    with open(out_dir, 'wb') as d:  ## Open temporary file as bytes
        d.write(output_stream.read())
    input_stream.close()
    return out_dir
Example #17
0
def test_bookmark_write_to_stream():
    stream = BytesIO()
    bm = Bookmark(NameObject("title"), NullObject(), NameObject(TF.FIT_V),
                  FloatObject(0))
    bm.write_to_stream(stream, None)
    stream.seek(0, 0)
    assert stream.read() == b"<<\n/Title title\n/Dest [ null /FitV 0 ]\n>>"
Example #18
0
    def _set_appearances_to_writer(self, writer: PdfFileWriter) -> None:
        catalog = writer._root_object
        # get the AcroForm tree
        if "/AcroForm" not in catalog:
            writer._root_object.update({NameObject("/AcroForm"): IndirectObject(len(writer._objects), 0, writer)})

        need_appearances = NameObject("/NeedAppearances")
        writer._root_object["/AcroForm"][need_appearances] = BooleanObject(True)
def pdf(request, template):
    #template = r'C:\Users\Mathi\Documents\Coding\PDF_Templates\Test_Contract.pdf'  # location of the pdf template

    outfile = r'C:\Users\Mathi\Documents\Coding\PDF_Templates\templates/test.pdf'  # location of the filled in pdf

    input_stream = open(
        template, "rb"
    )  # opens the template for reading in binary mode and returns it as a file object

    # PyPDF2 class that takes a file object or path to file (test), strict determines whether user should be warned of
    # all problems and also causes some correctable problems to be fatal. Initialises the PdfFileReader object.
    pdf_reader = PyPDF2.PdfFileReader(input_stream, strict=False)

    # Trailer is where all the file's metadata is stored, in a pdf the AcroForm contains the annotation fields
    # NeedAppearances needs to be true to enable the modification and setting of field value
    if "/AcroForm" in pdf_reader.trailer["/Root"]:
        pdf_reader.trailer["/Root"]["/AcroForm"].update(
            {NameObject("/NeedAppearances"): BooleanObject(True)})

    # We create a blank pdf page that will be writen
    pdf_writer = PyPDF2.PdfFileWriter()
    set_need_appearances_writer(pdf_writer)
    if "/AcroForm" in pdf_writer._root_object:
        # Acro form is form field, set needs appearances to fix printing issues
        pdf_writer._root_object["/AcroForm"].update(
            {NameObject("/NeedAppearances"): BooleanObject(True)})

    data_dict = {
        'numero_de_contrat#0': '12345\n',
        'numero_de_contrat#1': '12345\n',
        'Raison_Sociale': 'Dunder Mifflen\n',
        'Adresse': '1 paper drive, Paris\n',
        'SIREN': '1 paper drive, Paris\n',
        'Tel': '06.95.97.02.30\n',
    }

    # Create new page in this pdf we are writingr
    pdf_writer.addPage(pdf_reader.getPage(0))
    page = pdf_writer.getPage(0)
    pdf_writer.updatePageFormFieldValues(page, data_dict)
    for j in range(0, len(page['/Annots'])):
        writer_annot = page['/Annots'][j].getObject()
        for field in data_dict:
            # -----------------------------------------------------BOOYAH!
            if writer_annot.get('/T') == field:
                writer_annot.update({NameObject("/Ff"): NumberObject(1)})
            # -----------------------------------------------------
    output_stream = BytesIO()
    pdf_writer.write(output_stream)

    response = HttpResponse(output_stream.getvalue(),
                            content_type='application/pdf')
    response['Content-Disposition'] = 'inline; filename="completed.pdf"'
    input_stream.close()

    return FileResponse(output_stream, as_attachment=True, filename='test.pdf')
Example #20
0
def add_xobject_to_page(page, obj_id):
    res = page.setdefault(NameObject('/Resources'), DictionaryObject())
    xo = res.setdefault(NameObject('/XObject'), DictionaryObject())
    seq = 0
    while True:
        name = NameObject('/img_%s' % seq)
        if name not in xo:
            xo[name] = obj_id
            return name
        seq += 1
Example #21
0
def addHighlightToPage(highlight, page, output):
    '''
	Add the annotation object to the page
	'''
    highlight_ref = output._addObject(highlight)

    if "/Annots" in page:
        page[NameObject("/Annots")].append(highlight_ref)
    else:
        page[NameObject("/Annots")] = ArrayObject([highlight_ref])
    def addHighlightToPage(highlight, page, output):
        highlight_ref = output._addObject(highlight)
        print(highlight_ref)

        if "/Annots" in page:
            print("Annots in page")
            page[NameObject("/Annots")].append(highlight_ref)
        else:
            print("Annots not in page")
            page[NameObject("/Annots")] = ArrayObject([highlight_ref])
Example #23
0
def create_annot_box(x1, y1, x2, y2, meta, color=[1, 0, 0]):
    new_annot = DictionaryObject()

    new_annot.update({
        # NameObject("/P"): parent,
        NameObject("/F"):
        NumberObject(4),
        NameObject("/Type"):
        NameObject("/Annot"),
        NameObject("/Subtype"):
        NameObject("/Square"),
        NameObject("/T"):
        TextStringObject(meta["author"]),
        NameObject("/Contents"):
        TextStringObject(meta["contents"]),
        NameObject("/C"):
        ArrayObject([FloatObject(c) for c in color]),
        NameObject("/Rect"):
        ArrayObject([
            FloatObject(x1),
            FloatObject(y1),
            FloatObject(x2),
            FloatObject(y2)
        ]),
    })
    return new_annot
Example #24
0
    def addAttachment(self, name, data, subtype=None):
        """
        Add an attachment to the pdf. Supports adding multiple attachment, while respecting PDF/A rules.
        :param name: The name of the attachement
        :param data: The data of the attachement
        :param subtype: The mime-type of the attachement. This is required by PDF/A, but not essential otherwise.
        It should take the form of "/xxx#2Fxxx". E.g. for "text/xml": "/text#2Fxml"
        """
        adapted_subtype = subtype
        if subtype:
            # If we receive the subtype in an 'unformated' (mimetype) format, we'll try to convert it to a pdf-valid one
            if REGEX_SUBTYPE_UNFORMATED.match(subtype):
                adapted_subtype = '/' + subtype.replace('/', '#2F')

            if not REGEX_SUBTYPE_FORMATED.match(adapted_subtype):
                # The subtype still does not match the correct format, so we will not add it to the document
                _logger.warning(
                    "Attempt to add an attachment with the incorrect subtype '%s'. The subtype will be ignored.",
                    subtype)
                adapted_subtype = ''

        attachment = self._create_attachment_object({
            'filename': name,
            'content': data,
            'subtype': adapted_subtype,
        })
        if self._root_object.get('/Names') and self._root_object['/Names'].get(
                '/EmbeddedFiles'):
            names_array = self._root_object["/Names"]["/EmbeddedFiles"][
                "/Names"]
            names_array.extend([attachment.getObject()['/F'], attachment])
        else:
            names_array = ArrayObject()
            names_array.extend([attachment.getObject()['/F'], attachment])

            embedded_files_names_dictionary = DictionaryObject()
            embedded_files_names_dictionary.update(
                {NameObject("/Names"): names_array})
            embedded_files_dictionary = DictionaryObject()
            embedded_files_dictionary.update({
                NameObject("/EmbeddedFiles"):
                embedded_files_names_dictionary
            })
            self._root_object.update(
                {NameObject("/Names"): embedded_files_dictionary})

        if self._root_object.get('/AF'):
            attachment_array = self._root_object['/AF']
            attachment_array.extend([attachment])
        else:
            # Create a new object containing an array referencing embedded file
            # And reference this array in the root catalogue
            attachment_array = self._addObject(ArrayObject([attachment]))
            self._root_object.update({NameObject("/AF"): attachment_array})
Example #25
0
def update_checkbox_values(page, fields):
    for j in range(0, len(page["/Annots"])):
        writer_annot = page["/Annots"][j].getObject()
        for field in fields:
            if writer_annot.get("/T") == field:
                writer_annot.update(
                    {
                        NameObject("/V"): NameObject(fields[field]),
                        NameObject("/AS"): NameObject(fields[field]),
                    }
                )
Example #26
0
 def _ubl_add_xml_in_pdf_buffer(self, xml_string, xml_filename, buffer):
     # Add attachment to PDF content.
     reader = PdfFileReader(buffer)
     writer = PdfFileWriter()
     writer.appendPagesFromReader(reader)
     writer.addAttachment(xml_filename, xml_string)
     # show attachments when opening PDF
     writer._root_object.update(
         {NameObject("/PageMode"): NameObject("/UseAttachments")})
     new_buffer = BytesIO()
     writer.write(new_buffer)
     return new_buffer
Example #27
0
def set_need_appearances_writer(writer):
    try:
        catalog = writer._root_object
        # get the AcroForm tree and add "/NeedAppearances attribute
        if "/AcroForm" not in catalog:
            writer._root_object.update({NameObject("/AcroForm"): IndirectObject(len(writer._objects), 0, writer)})
        need_appearances = NameObject("/NeedAppearances")
        writer._root_object["/AcroForm"][need_appearances] = BooleanObject(True)
    except Exception as e:
        print('set_need_appearances_writer() catch : ', repr(e))

    return writer
Example #28
0
def createHighlight(bbox=(0, 0, 1, 1),
                    contents="",
                    color=[1, 1, 0],
                    author="iwasakishuto(@cabernet_rock)"):
    """Create a Highlight

    Args:
        bbox (tuple)   : a bounding box showing the location of highlight.
        contents (str) : Text comments for a highlight label.
        color (list)   : Highlight color. Defaults to ``[1,1,0]``. (yellow)
        author (str)   : Who wrote the annotation (comment). Defaults to ``"iwasakishuto(@cabernet_rock)"`` .

    Returns:
        DictionaryObject: Highlight information.

    Examples:
        >>> from gummy.utils import createHighlight, addHighlightToPage
        >>> from PyPDF2 import PdfFileWriter, PdfFileReader
        >>> page_no = 0
        >>> pdfOutput = PdfFileWriter()
        >>> with open("input.pdf", mode="rb") as inPdf:
        ...     pdfInput = PdfFileReader(inPdf)
        ...     page = pdfInput.getPage(page_no)
        ...     highlight = createHighlight(bbox=(10,10,90,90), contents="COMMENT", color=(1,1,0))
        ...     addHighlightToPage(highlight, page, pdfOutput)
        ...     pdfOutput.addPage(page)
        ...     with open("output.pdf", mode="wb") as outPdf:
        ...         pdfOutput.write(outPdf)
    """
    from PyPDF2.generic import (DictionaryObject, NumberObject, FloatObject,
                                NameObject, TextStringObject, ArrayObject)
    x1, y1, x2, y2 = bbox
    newHighlight = DictionaryObject()
    newHighlight.update({
        NameObject("/F"):
        NumberObject(4),
        NameObject("/Type"):
        NameObject("/Annot"),
        NameObject("/Subtype"):
        NameObject("/Highlight"),
        NameObject("/T"):
        TextStringObject(author),
        NameObject("/Contents"):
        TextStringObject(contents),
        NameObject("/C"):
        ArrayObject([FloatObject(c) for c in color]),
        NameObject("/Rect"):
        ArrayObject([FloatObject(e) for e in bbox]),
        NameObject("/QuadPoints"):
        ArrayObject([FloatObject(e)
                     for e in [x1, y2, x2, y2, x1, y1, x2, y1]]),
    })
    return newHighlight
Example #29
0
def pdf(request):
    template = os.path.join(BASE_DIR, "family1.pdf")

    outfile = os.path.join(BASE_DIR, "sample.pdf")

    input_stream = open(template, "rb")
    pdf_reader = PyPDF2.PdfFileReader(input_stream, strict=False)
    if "/AcroForm" in pdf_reader.trailer["/Root"]:
        pdf_reader.trailer["/Root"]["/AcroForm"].update(
            {NameObject("/NeedAppearances"): BooleanObject(True)})

    pdf_writer = PyPDF2.PdfFileWriter()
    set_need_appearances_writer(pdf_writer)
    if "/AcroForm" in pdf_writer._root_object:
        # Acro form is form field, set needs appearances to fix printing issues
        pdf_writer._root_object["/AcroForm"].update(
            {NameObject("/NeedAppearances"): BooleanObject(True)})

    data_dict = {
        'Fid': 'John\n',
        'Fname1': 'Smith\n',
        'Faadhar': '[email protected]\n',
        'Fcontact': '889-998-9967\n',
        'Faddress1': 'Amazing Inc.\n',
        'Faddress2': 'Dev\n',
        'Faddress3': '123 Main Way\n',
        'Fration': 'Johannesburg\n',
        'Farogya': 'New Mexico\n',
        'Faadhar1': 96705,
        'from_date': 'USA\n',
        'to_date': 'Who cares...\n'
    }

    pdf_writer.addPage(pdf_reader.getPage(0))
    page = pdf_writer.getPage(0)
    pdf_writer.updatePageFormFieldValues(page, data_dict)
    for j in range(0, len(page['/Annots'])):
        writer_annot = page['/Annots'][j].getObject()
        for field in data_dict:
            # -----------------------------------------------------BOOYAH!
            if writer_annot.get('/T') == field:
                writer_annot.update({NameObject("/Ff"): NumberObject(1)})
            # -----------------------------------------------------
    output_stream = BytesIO()
    pdf_writer.write(output_stream)

    response = HttpResponse(output_stream.getvalue(),
                            content_type='application/pdf')
    response['Content-Disposition'] = 'inline; filename="completed.pdf"'
    input_stream.close()

    return response
Example #30
0
def meta(pa, ti, au, fi, loo, ho, loi):
    OUTPUT = ti + '.pdf'
    INPUTS = [
        ti + '.pdf',
    ]

    if au == None:
        au = ''
    else:
        pass

    output = PdfFileWriter()

    infoDict = output._info.getObject()
    infoDict.update({
        NameObject('/Title'): createStringObject(ti),
        NameObject('/Author'): createStringObject(str(au)),
        # NameObject('/Subject'): createStringObject(su),
        #NameObject('/'): createStringObject('Fit'),
        # NameObject('/Fit'): createStringObject('Fit-to-page')
    })

    inputs = [PdfFileReader(open(i, "rb")) for i in INPUTS]
    for input in inputs:
        for page in range(input.getNumPages()):
            output.addPage(input.getPage(page))
            #output.addLink(page,0,rect='[0,0,0,0]',border=None,fit='/Fit')
    if os.path.isdir(ho + '/' + loo) == True:
        os.chdir(ho + '/' + loo)
    elif os.path.isdir(ho + '/' + loo) == False:
        os.chdir(loo)
    #pdf.generic.Destination(title='test',page=1,typ='/Fit')
    output.setPageLayout('/SinglePage')
    outputStream = open(OUTPUT, 'wb')
    #output.addLink(0,0,rect='[0,0,0,0]',border=None,fit='/Fit')
    output.write(outputStream)
    outputStream.close()

    if os.path.isdir(loi) == True:
        os.chdir(loi)
    elif os.path.isdir(loi) == False:
        os.chdir(ho + '/' + loi)
    os.remove(ti + '.pdf')
    if os.name == 'posix':

        subprocess.call(ho + 'cpdf/mac/cpdf.sh -fit-window true ' + ho + '/' +
                        loo + ti + ' -o ' + ho + '/' + loo + ti,
                        shell=True)
    elif os.name == 'nt':
        subprocess.call(ho + 'cpdf/win/cpdf.exe -fit-window true ' + ho + '/' +
                        loo + ti + ' -o ' + ho + '/' + loo + ti,
                        shell=True)