def add_signature(request, registration_id):

    registration = get_object_or_404(models.Registration, pk=registration_id)
    str_key = str(registration.key)

    path_convention = settings.MEDIA_ROOT+'/registration_data/conventions/'+str_key+'/convention_'+str_key+'.pdf'
    path_signature = settings.MEDIA_ROOT+'/signature/signature_only.pdf'


    output = PdfFileWriter()
    input1 = PdfFileReader(file(path_convention, "rb"))
    watermark = PdfFileReader(file(path_signature, "rb"))

    input1.getPage(2).mergePage(watermark.getPage(0))

    output.addPage(input1.getPage(0))
    output.addPage(input1.getPage(1))
    output.addPage(input1.getPage(2))
    # finally, write "output" to document-output.pdf
    outputStream = file(settings.MEDIA_ROOT+'/registration_data/conventions/'+str_key+'/convention_'+str_key+'_final.pdf', "wb")
    output.write(outputStream)
    outputStream.close()

    registration.convention.name = 'registration_data/conventions/'+str_key+'/convention_'+str_key+'_final.pdf'
    registration.state=3
    registration.save()
    mail.send_convocation(registration)

    return redirect('registration-archive-list')
예제 #2
0
        def getProjectFile(self):
            if not self.getDirectories():
                return None
            import json
            data = file(
                os.path.normpath(
                    os.path.join(sublime.packages_path(), '..',
                                 local_data_sess, 'Session.sublime_session')),
                'r').read()
            data = data.replace('\t', ' ')
            data = json.loads(data, strict=False)

            projects = data['workspaces']['recent_workspaces']

            if os.path.lexists(
                    os.path.join(sublime.packages_path(), '..',
                                 local_data_sess,
                                 'Auto Save Session.sublime_session')):
                data = file(
                    os.path.normpath(
                        os.path.join(sublime.packages_path(), '..',
                                     local_data_sess,
                                     'Auto Save Session.sublime_session')),
                    'r').read()
                data = data.replace('\t', ' ')
                data = json.loads(data, strict=False)
                if 'workspaces' in data and 'recent_workspaces' in data[
                        'workspaces'] and data['workspaces'][
                            'recent_workspaces']:
                    projects += data['workspaces']['recent_workspaces']
                projects = list(set(projects))
            for project_file in projects:
                project_file = re.sub(r'^/([^/])/', '\\1:/', project_file)
                project_json = json.loads(file(project_file, 'r').read(),
                                          strict=False)
                if 'folders' in project_json:
                    folders = project_json['folders']
                    found_all = True
                    for directory in self.getDirectories():
                        found = False
                        for folder in folders:
                            folder_path = re.sub(r'^/([^/])/', '\\1:/',
                                                 folder['path'])
                            if folder_path == directory.replace('\\', '/'):
                                found = True
                                break
                        if found == False:
                            found_all = False
                            break
                if found_all:
                    return project_file
            return None
예제 #3
0
파일: merger.py 프로젝트: Gluejar/PyPDF2
    def write(self, fileobj):
        """
        Writes all data that has been merged to the given output file.

        :param fileobj: Output file. Can be a filename or any kind of
            file-like object.
        """
        my_file = False
        if type(fileobj) in (str, str):
            fileobj = file(fileobj, 'wb')
            my_file = True


        # Add pages to the PdfFileWriter
        # The commented out line below was replaced with the two lines below it to allow PdfFileMerger to work with PyPdf 1.13
        for page in self.pages:
            self.output.addPage(page.pagedata)
            page.out_pagedata = self.output.getReference(self.output._pages.getObject()["/Kids"][-1].getObject())
            #idnum = self.output._objects.index(self.output._pages.getObject()["/Kids"][-1].getObject()) + 1
            #page.out_pagedata = IndirectObject(idnum, 0, self.output)

        # Once all pages are added, create bookmarks to point at those pages
        self._write_dests()
        self._write_bookmarks()
        
        # Write the output to the file   
        self.output.write(fileobj)
        
        if my_file:
            fileobj.close()
예제 #4
0
    def write(self, fileobj):
        """
        Writes all data that has been merged to the given output file.

        :param fileobj: Output file. Can be a filename or any kind of
            file-like object.
        """
        my_file = False
        if isString(fileobj):
            fileobj = file(fileobj, 'wb')
            my_file = True

        # Add pages to the PdfFileWriter
        # The commented out line below was replaced with the two lines below it to allow PdfFileMerger to work with PyPdf 1.13
        for page in self.pages:
            self.output.addPage(page.pagedata)
            page.out_pagedata = self.output.getReference(
                self.output._pages.getObject()["/Kids"][-1].getObject())
            #idnum = self.output._objects.index(self.output._pages.getObject()["/Kids"][-1].getObject()) + 1
            #page.out_pagedata = IndirectObject(idnum, 0, self.output)

        # Once all pages are added, create bookmarks to point at those pages
        self._write_dests()
        self._write_bookmarks()

        # Write the output to the file
        self.output.write(fileobj)

        if my_file:
            fileobj.close()
예제 #5
0
def searchable_pdf(pdf_file):
    input_file_name = os.path.basename(pdf_file).split(".")[0]
    output_file_name = input_file_name + "_converted.pdf"
    images = convert_from_path(pdf_file)
    pdf_pages = [pytesseract.image_to_pdf_or_hocr(image, extension='pdf') for image in images]
    with open(output_file_name, "wb") as f:
        f.write(pdf_pages[0])
    if len(pdf_pages)>1:
        for i in range(len(pdf_pages)-1):
            with open("append.pdf", "wb") as f:
                f.write(pdf_pages[i+1])
            merger = PdfFileMerger()
            merger.append(PdfFileReader(file(output_file_name, 'rb')))
            merger.append(PdfFileReader(file("append.pdf", 'rb')))
            merger.write(output_file_name)
    os.remove("append.pdf")
    print("searchable pdf created")
예제 #6
0
 def getProjectJson(self):
   if not self.hasOpenedProject():
     return None
   if int(sublime.version()) < 3000:
     import json
     return json.loads(file(self.getProjectFile(), 'r').read(), strict=False)
   else:
     return sublime.active_window().project_data()
예제 #7
0
파일: configure.py 프로젝트: Elef-PING/Elef
def getConfigure():
    cf = None
    try:
        f = file(CONFIGURE_PATH, "r")
        cf = json.load(f)
    except:
        cf = {NAME_KEY: None, LANG_KEY: None}
    return cf
예제 #8
0
def load():
    f, r = None, None
    try:
        f = file(CALENDAR_PATH, "r")
        r = pck.load(f)
    except:
        r = list()
    return r
예제 #9
0
 def getProjectJson(self):
     if not self.hasOpenedProject():
         return None
     if int(sublime.version()) < 3000:
         import json
         return json.loads(file(self.getProjectFile(), 'r').read(),
                           strict=False)
     else:
         return sublime.active_window().project_data()
예제 #10
0
    def upload_object(self, file_path, container, object_name, extra=None,
                      verify_hash=True, ex_blob_type=None, ex_use_lease=False):
        """
        Upload an object currently located on a disk.

        @inherits: :class:`StorageDriver.upload_object`

        :param ex_blob_type: Storage class
        :type ex_blob_type: ``str``

        :param ex_use_lease: Indicates if we must take a lease before upload
        :type ex_use_lease: ``bool``
        """

        if ex_blob_type is None:
            ex_blob_type = self.ex_blob_type

        # Get the size of the file
        file_size = os.stat(file_path).st_size

        # The presumed size of the object
        object_size = file_size

        self._check_values(ex_blob_type, file_size)

        with file(file_path, 'rb') as file_handle:
            iterator = iter(file_handle)

            # If size is greater than 64MB or type is Page, upload in chunks
            if ex_blob_type == 'PageBlob' or file_size > AZURE_BLOCK_MAX_SIZE:
                # For chunked upload of block blobs, the initial size must
                # be 0.
                if ex_blob_type == 'BlockBlob':
                    object_size = None

                object_path = self._get_object_path(container, object_name)

                upload_func = self._upload_in_chunks
                upload_func_kwargs = {'iterator': iterator,
                                      'object_path': object_path,
                                      'blob_type': ex_blob_type,
                                      'lease': None}
            else:
                upload_func = self._stream_data
                upload_func_kwargs = {'iterator': iterator,
                                      'chunked': False,
                                      'calculate_hash': verify_hash}

            return self._put_object(container=container,
                                    object_name=object_name,
                                    object_size=object_size,
                                    upload_func=upload_func,
                                    upload_func_kwargs=upload_func_kwargs,
                                    file_path=file_path, extra=extra,
                                    verify_hash=verify_hash,
                                    blob_type=ex_blob_type,
                                    use_lease=ex_use_lease)
예제 #11
0
    def upload_object(self, file_path, container, object_name, extra=None,
                      verify_hash=True, ex_blob_type=None, ex_use_lease=False):
        """
        Upload an object currently located on a disk.

        @inherits: L{StorageDriver.upload_object}

        @param ex_blob_type: Storage class
        @type ex_blob_type: C{str}

        @param ex_use_lease: Indicates if we must take a lease before upload
        @type ex_use_lease: C{bool}
        """

        if ex_blob_type is None:
            ex_blob_type = self.ex_blob_type

        # Get the size of the file
        file_size = os.stat(file_path).st_size

        # The presumed size of the object
        object_size = file_size

        self._check_values(ex_blob_type, file_size)

        with file(file_path, 'rb') as file_handle:
            iterator = iter(file_handle)

            # If size is greater than 64MB or type is Page, upload in chunks
            if ex_blob_type == 'PageBlob' or file_size > AZURE_BLOCK_MAX_SIZE:
                # For chunked upload of block blobs, the initial size must
                # be 0.
                if ex_blob_type == 'BlockBlob':
                    object_size = None

                object_path = self._get_object_path(container, object_name)

                upload_func = self._upload_in_chunks
                upload_func_kwargs = {'iterator': iterator,
                                      'object_path': object_path,
                                      'blob_type': ex_blob_type,
                                      'lease': None}
            else:
                upload_func = self._stream_data
                upload_func_kwargs = {'iterator': iterator,
                                      'chunked': False,
                                      'calculate_hash': verify_hash}

            return self._put_object(container=container,
                                    object_name=object_name,
                                    object_size=object_size,
                                    upload_func=upload_func,
                                    upload_func_kwargs=upload_func_kwargs,
                                    file_path=file_path, extra=extra,
                                    verify_hash=verify_hash,
                                    blob_type=ex_blob_type,
                                    use_lease=ex_use_lease)
예제 #12
0
파일: setup.py 프로젝트: PoorHttp/PoorWSGI
 def page(self, in_name, out_name=None):
     """Generate page."""
     if out_name is None:
         out_name = in_name
     if call([
             'jinja24doc', '-v', '--var',
             'public=%s' % self.public,
             '_%s.html' % in_name, 'doc'
     ],
             stdout=file(self.html_temp + '/%s.html' % out_name, 'w')):
         raise IOError(1, 'jinja24doc failed')
예제 #13
0
def save_cover(cpl_img_url):
    req = Request(cpl_img_url, None, headers)
    atc_file = urlopen(req)
    atc_out = atc_file.readall().decode(encoding='utf-8')
    div_idx = atc_out.find(r'<div class="cover-content">') 
    if div_idx != -1:
        img_begin = atc_out.find(r'<img src="', div_idx)  
        if img_begin != -1:
            img_end = atc_out.find(r'" alt title')
            if img_end != -1:
                img_url = atc_out[img_begin + len(r'<img src="') : img_end]
                img_data = urlopen(img_url).read()
                f = file(g_issue_title + ".jpg", "wb")
                f.write(img_data)
                f.close()
예제 #14
0
    def getProjectFile(self):
      if not self.getDirectories():
        return None
      import json
      data = file(os.path.normpath(os.path.join(sublime.packages_path(), '..', local_data_sess, 'Session.sublime_session')), 'r').read()
      data = data.replace('\t', ' ')
      data = json.loads(data, strict=False)

      projects = data['workspaces']['recent_workspaces']

      if os.path.lexists(os.path.join(sublime.packages_path(), '..', local_data_sess, 'Auto Save Session.sublime_session')):
        data = file(os.path.normpath(os.path.join(sublime.packages_path(), '..', local_data_sess, 'Auto Save Session.sublime_session')), 'r').read()
        data = data.replace('\t', ' ')
        data = json.loads(data, strict=False)
        if 'workspaces' in data and 'recent_workspaces' in data['workspaces'] and data['workspaces']['recent_workspaces']:
          projects += data['workspaces']['recent_workspaces']
        projects = list(set(projects))
      for project_file in projects:
        project_file = re.sub(r'^/([^/])/', '\\1:/', project_file);
        project_json = json.loads(file(project_file, 'r').read(), strict=False)
        if 'folders' in project_json:
          folders = project_json['folders']
          found_all = True
          for directory in self.getDirectories():
            found = False
            for folder in folders:
              folder_path = re.sub(r'^/([^/])/', '\\1:/', folder['path']);
              if folder_path == directory.replace('\\', '/'):
                found = True
                break;
            if found == False:
              found_all = False
              break;
        if found_all:
          return project_file
      return None
예제 #15
0
def reduceMergeFile2(files):
    """
        input:
        files: a list of file to be reduced and merged
    """
    openfile = map(lambda x: file(x, 'rb'), files)
    keepPages = reduceFile(files, False)

    pageRages = map(lambda x: list(ranges(x)), keepPages)
    merger = PdfFileMerger()
    for i in range(len(keepPages)):
        print("Merging file \"{}\"...".format(files[i]))
        for pageRage in pageRages[i]:
            merger.append(fileobj=openfile[i], pages=pageRage)

    newfile = "reduced_and_merged.pdf"
    with open(newfile, 'wb') as f:
        merger.write(f)
예제 #16
0
def mergeFile(files):
    """
        input:
        files: a list of file to merge
    """
    merger = PdfFileMerger()
    for f in files:
        print("Merging file \"{}\"...".format(f))
        # check if file can be opened
        try:
            # read and merge pdf with pypdf2
            merger.append(PdfFileReader(file(f, 'rb')))
        except IOError as e:
            print("I/O error: Please input an existing pdf file:", fp)
            raise
        except:
            print(fp, ": Unexpected error - ", sys.exc_info()[0])
            raise
    # TODO: need to change output path later
    merger.write("output/output.pdf")
예제 #17
0
파일: mobi.py 프로젝트: msdemlei/rst2epub2
def test():
    with io.file("data/test.html", mode="r", encoding="utf8") as test_file:
        TEST_HTML = test_file.read()  # noqa

    book = MobiComicBook(add_jquery=True)
    title = "Test Comic35"
    book.set_title(title)
    cov_image = "data/little-nemo-19051015-l.jpeg"
    book.add_cover(cov_image, title=title)
    page = book.add_page()
    page.add_bg_image("data/little-nemo-19051015-l.jpeg", "img1.jpeg")
    # page.add_mag('foo_id_parent', 'foo_id', 20, 20, 30, 60, 30, 30)

    # target_left, target_top, target_width, target_height,
    page.auto_target(
        50,
        1 * 100.0 / 6,
        50,
        100.0 / 6,
        zoom_factor=3,
        post_data='<p class="textzoom">TEST DATA</p>',
    )

    for col in range(4):
        # target_left, target_top, target_width, target_height,
        page.auto_target(col * 25, 500.0 / 6, 25, 100.0 / 6, zoom_factor=2)

    h1 = page.add_html("1.html")
    book.add_spine_item(h1)

    page.add_bg_image("data/little-nemo-19051105-l.jpeg", "img3.jpeg")
    h3 = book.add_html("", "3.html", TEST_HTML)
    book.add_spine_item(h3)
    directory = "/tmp/mobitest"
    book.create_book(directory)
    book.create_archive(directory, directory + ".epub")
예제 #18
0
파일: merger.py 프로젝트: Rob1080/PyPDF2
    def merge(self, position, fileobj, bookmark=None, pages=None, import_bookmarks=True):
        """
        >>> merge(position, file, bookmark=None, pages=None, import_bookmarks=True)
        
        Merges the pages from the source document specified by "file" into the output
        file at the page number specified by "position".
        
        Optionally, you may specify a bookmark to be applied at the beginning of the 
        included file by supplying the text of the bookmark in the "bookmark" parameter.
        
        You may prevent the source document's bookmarks from being imported by
        specifying "import_bookmarks" as False.
        
        You may also use the "pages" parameter to merge only the specified range of 
        pages from the source document into the output document.
        """
        
        # This parameter is passed to self.inputs.append and means
        # that the stream used was created in this method.
        my_file = False
        
        # If the fileobj parameter is a string, assume it is a path
        # and create a file object at that location. If it is a file,
        # copy the file's contents into a StringIO stream object; if 
        # it is a PdfFileReader, copy that reader's stream into a 
        # StringIO stream.
        # If fileobj is none of the above types, it is not modified
        if type(fileobj) in (str, str):
            fileobj = file(fileobj, 'rb')
            my_file = True
        elif isinstance(fileobj, file):
            fileobj.seek(0)
            filecontent = fileobj.read()
            fileobj = StringIO(filecontent)
            my_file = True
        elif isinstance(fileobj, PdfFileReader):
            orig_tell = fileobj.stream.tell()   
            fileobj.stream.seek(0)
            filecontent = StringIO(fileobj.stream.read())
            fileobj.stream.seek(orig_tell) # reset the stream to its original location
            fileobj = filecontent
            my_file = True
            
        # Create a new PdfFileReader instance using the stream
        # (either file or StringIO) created above
        pdfr = PdfFileReader(fileobj, strict=self.strict)
        
        # Find the range of pages to merge
        if pages == None:
            pages = (0, pdfr.getNumPages())
        elif type(pages) in (int, float, str, str):
            raise TypeError('"pages" must be a tuple of (start, end)')
        
        srcpages = []
        if bookmark:
            bookmark = Bookmark(TextStringObject(bookmark), NumberObject(self.id_count), NameObject('/Fit'))
        
        outline = []
        if import_bookmarks:
            outline = pdfr.getOutlines()
            outline = self._trim_outline(pdfr, outline, pages)
        
        if bookmark:
            self.bookmarks += [bookmark, outline]
        else:
            self.bookmarks += outline
        
        dests = pdfr.namedDestinations
        dests = self._trim_dests(pdfr, dests, pages)
        self.named_dests += dests
        
        # Gather all the pages that are going to be merged
        for i in range(*pages):
            pg = pdfr.getPage(i)
            
            id = self.id_count
            self.id_count += 1
            
            mp = _MergedPage(pg, pdfr, id)
            
            srcpages.append(mp)

        self._associate_dests_to_pages(srcpages)
        self._associate_bookmarks_to_pages(srcpages)
            
        
        # Slice to insert the pages at the specified position
        self.pages[position:position] = srcpages
        
        # Keep track of our input files so we can close them later
        self.inputs.append((fileobj, pdfr, my_file))
    def print_batch_label(self):
        final_pickings = []
        fd_final, result = mkstemp()
        output = PdfFileWriter()
        picking_ids = []
        sorted_pickings = []

        for picking in self.env['stock.picking'].browse(
                self._context['active_ids']):
            if not picking.label_generated:
                continue

            picking_ids.append(picking)

        # sorts and returns the picking ids with the product's default code
        if picking_ids:
            sorted_pickings = self.get_sorted_pickings(picking_ids)

        for picking in sorted_pickings:
            fd, file_name = mkstemp()
            attachment_id = self.env['ir.attachment'].search([
                ('res_model', '=', 'stock.picking'),
                ('res_id', '=', picking.id)
            ])
            if not attachment_id:
                continue
            os.write(fd, base64.decodestring(attachment_id[0].datas))
            pdf = PdfFileReader(file(file_name, "rb"))
            pgcnt = pdf.getNumPages()
            for i in range(0, pgcnt):
                output.addPage(pdf.getPage(i))
            final_pickings.append(picking)
        if sorted_pickings:
            binary_pdfs = output
            outputStream = file(result, "wb")
            output.write(outputStream)
            outputStream.close()
            f = open(result, "rb")
            batch = f.read()
            filename = str(datetime.datetime.now()).replace('-', '') + '.pdf'
            batch_id = self.env['batch.file'].create({
                'file':
                base64.encodestring(batch),
                'filename':
                filename,
            })
            action = {
                'name':
                'Generated Batch File',
                'type':
                'ir.actions.act_url',
                'url':
                "web/content/?model=batch.file&id=" + str(batch_id.id) +
                "&filename_field=filename&field=file&download=true&filename=" +
                filename,
                'target':
                'self',
            }
            for picking in final_pickings:
                if picking.label_generated and picking.shipment_created:
                    picking.label_printed = True

            return action
예제 #20
0
파일: merger.py 프로젝트: Gluejar/PyPDF2
    def merge(self, position, fileobj, bookmark=None, pages=None, import_bookmarks=True):
        """
        Merges the pages from the given file into the output file at the
        specified page number.

        :param int position: The *page number* to insert this file. File will
            be inserted after the given number.

        :param fileobj: A File Object or an object that supports the standard read
            and seek methods similar to a File Object. Could also be a
            string representing a path to a PDF file.
        
        :param str bookmark: Optionally, you may specify a bookmark to be applied at
            the beginning of the included file by supplying the text of the bookmark.

        :param pages: can be a :ref:`Page Range <page-range>` or a ``(start, stop[, step])`` tuple
            to merge only the specified range of pages from the source
            document into the output document.
        
        :param bool import_bookmarks: You may prevent the source document's bookmarks
            from being imported by specifying this as ``False``.
        """
        
        # This parameter is passed to self.inputs.append and means
        # that the stream used was created in this method.
        my_file = False
        
        # If the fileobj parameter is a string, assume it is a path
        # and create a file object at that location. If it is a file,
        # copy the file's contents into a BytesIO (or StreamIO) stream object; if 
        # it is a PdfFileReader, copy that reader's stream into a 
        # BytesIO (or StreamIO) stream.
        # If fileobj is none of the above types, it is not modified
        if type(fileobj) == string_type:
            fileobj = file(fileobj, 'rb')
            my_file = True
        elif isinstance(fileobj, file):
            fileobj.seek(0)
            filecontent = fileobj.read()
            fileobj = StreamIO(filecontent)
            my_file = True
        elif isinstance(fileobj, PdfFileReader):
            orig_tell = fileobj.stream.tell()   
            fileobj.stream.seek(0)
            filecontent = StreamIO(fileobj.stream.read())
            fileobj.stream.seek(orig_tell) # reset the stream to its original location
            fileobj = filecontent
            my_file = True
            
        # Create a new PdfFileReader instance using the stream
        # (either file or BytesIO or StringIO) created above
        pdfr = PdfFileReader(fileobj, strict=self.strict)
        
        # Find the range of pages to merge.
        if pages == None:
            pages = (0, pdfr.getNumPages())
        elif isinstance(pages, PageRange):
            pages = pages.indices(pdfr.getNumPages())
        elif not isinstance(pages, tuple):
            raise TypeError('"pages" must be a tuple of (start, stop[, step])')
        
        srcpages = []
        if bookmark:
            bookmark = Bookmark(TextStringObject(bookmark), NumberObject(self.id_count), NameObject('/Fit'))
        
        outline = []
        if import_bookmarks:
            outline = pdfr.getOutlines()
            outline = self._trim_outline(pdfr, outline, pages)
        
        if bookmark:
            self.bookmarks += [bookmark, outline]
        else:
            self.bookmarks += outline
        
        dests = pdfr.namedDestinations
        dests = self._trim_dests(pdfr, dests, pages)
        self.named_dests += dests
        
        # Gather all the pages that are going to be merged
        for i in range(*pages):
            pg = pdfr.getPage(i)
            
            id = self.id_count
            self.id_count += 1
            
            mp = _MergedPage(pg, pdfr, id)
            
            srcpages.append(mp)

        self._associate_dests_to_pages(srcpages)
        self._associate_bookmarks_to_pages(srcpages)
            
        
        # Slice to insert the pages at the specified position
        self.pages[position:position] = srcpages
        
        # Keep track of our input files so we can close them later
        self.inputs.append((fileobj, pdfr, my_file))
예제 #21
0
def printMeta(fileName):
    pdfFile = PdfFileReader(file(fileName, 'rb'))
    docInfo = pdfFile.getDocumentInfo()
    print('[*] PDF MetaData For: ' + str(fileName))
    for metaItem in docInfo:
        print(('[+] ' + metaItem + ':' + docInfo[metaItem]))
예제 #22
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
예제 #23
0
 def save(self):
     l = list(load())
     l.append(self)
     f = file(CALENDAR_PATH, "w")
     pck.dump(l, f)
    def merge(self,
              position,
              fileobj,
              bookmark=None,
              pages=None,
              import_bookmarks=True):
        """
        >>> merge(position, file, bookmark=None, pages=None, import_bookmarks=True)
        
        Merges the pages from the source document specified by "file" into the output
        file at the page number specified by "position".
        
        Optionally, you may specify a bookmark to be applied at the beginning of the 
        included file by supplying the text of the bookmark in the "bookmark" parameter.
        
        You may prevent the source document's bookmarks from being imported by
        specifying "import_bookmarks" as False.
        
        The optional "pages" parameter can be a PageRange or a 
        (start, stop[, step]) tuple to merge only the specified range of pages
        from the source document into the output document.
        """

        # This parameter is passed to self.inputs.append and means
        # that the stream used was created in this method.
        my_file = False

        # If the fileobj parameter is a string, assume it is a path
        # and create a file object at that location. If it is a file,
        # copy the file's contents into a BytesIO (or StreamIO) stream object; if
        # it is a PdfFileReader, copy that reader's stream into a
        # BytesIO (or StreamIO) stream.
        # If fileobj is none of the above types, it is not modified
        if type(fileobj) == string_type:
            fileobj = file(fileobj, 'rb')
            my_file = True
        elif isinstance(fileobj, file):
            fileobj.seek(0)
            filecontent = fileobj.read()
            fileobj = StreamIO(filecontent)
            my_file = True
        elif isinstance(fileobj, PdfFileReader):
            orig_tell = fileobj.stream.tell()
            fileobj.stream.seek(0)
            filecontent = StreamIO(fileobj.stream.read())
            fileobj.stream.seek(
                orig_tell)  # reset the stream to its original location
            fileobj = filecontent
            my_file = True

        # Create a new PdfFileReader instance using the stream
        # (either file or BytesIO or StringIO) created above
        pdfr = PdfFileReader(fileobj, strict=self.strict)

        # Find the range of pages to merge.
        if pages == None:
            pages = (0, pdfr.getNumPages())
        elif isinstance(pages, PageRange):
            pages = pages.indices(pdfr.getNumPages())
        elif not isinstance(pages, tuple):
            raise TypeError('"pages" must be a tuple of (start, stop[, step])')

        srcpages = []
        if bookmark:
            bookmark = Bookmark(TextStringObject(bookmark),
                                NumberObject(self.id_count),
                                NameObject('/Fit'))

        outline = []
        if import_bookmarks:
            outline = pdfr.getOutlines()
            outline = self._trim_outline(pdfr, outline, pages)

        if bookmark:
            self.bookmarks += [bookmark, outline]
        else:
            self.bookmarks += outline

        dests = pdfr.namedDestinations
        dests = self._trim_dests(pdfr, dests, pages)
        self.named_dests += dests

        # Gather all the pages that are going to be merged
        for i in range(*pages):
            pg = pdfr.getPage(i)

            id = self.id_count
            self.id_count += 1

            mp = _MergedPage(pg, pdfr, id)

            srcpages.append(mp)

        self._associate_dests_to_pages(srcpages)
        self._associate_bookmarks_to_pages(srcpages)

        # Slice to insert the pages at the specified position
        self.pages[position:position] = srcpages

        # Keep track of our input files so we can close them later
        self.inputs.append((fileobj, pdfr, my_file))
예제 #25
0
    def merge(self,
              position,
              fileobj,
              bookmark=None,
              pages=None,
              import_bookmarks=True):
        """
        Merges the pages from the given file into the output file at the
        specified page number.

        :param int position: The *page number* to insert this file. File will
            be inserted after the given number.

        :param fileobj: A File Object or an object that supports the standard read
            and seek methods similar to a File Object. Could also be a
            string representing a path to a PDF file.

        :param str bookmark: Optionally, you may specify a bookmark to be applied at
            the beginning of the included file by supplying the text of the bookmark.

        :param pages: can be a :ref:`Page Range <page-range>` or a ``(start, stop[, step])`` tuple
            to merge only the specified range of pages from the source
            document into the output document.

        :param bool import_bookmarks: You may prevent the source document's bookmarks
            from being imported by specifying this as ``False``.
        """

        # This parameter is passed to self.inputs.append and means
        # that the stream used was created in this method.
        my_file = False

        # If the fileobj parameter is a string, assume it is a path
        # and create a file object at that location. If it is a file,
        # copy the file's contents into a BytesIO (or StreamIO) stream object; if
        # it is a PdfFileReader, copy that reader's stream into a
        # BytesIO (or StreamIO) stream.
        # If fileobj is none of the above types, it is not modified
        decryption_key = None
        if isString(fileobj):
            fileobj = file(fileobj, 'rb')
            my_file = True
        elif hasattr(fileobj, "seek") and hasattr(fileobj, "read"):
            fileobj.seek(0)
            filecontent = fileobj.read()
            fileobj = StreamIO(filecontent)
            my_file = True
        elif isinstance(fileobj, PdfFileReader):
            orig_tell = fileobj.stream.tell()
            fileobj.stream.seek(0)
            filecontent = StreamIO(fileobj.stream.read())
            fileobj.stream.seek(
                orig_tell)  # reset the stream to its original location
            fileobj = filecontent
            if hasattr(fileobj, '_decryption_key'):
                decryption_key = fileobj._decryption_key
            my_file = True

        # Create a new PdfFileReader instance using the stream
        # (either file or BytesIO or StringIO) created above
        pdfr = PdfFileReader(fileobj, strict=self.strict)
        if decryption_key is not None:
            pdfr._decryption_key = decryption_key

        # Find the range of pages to merge.
        if pages == None:
            pages = (0, pdfr.getNumPages())
        elif isinstance(pages, PageRange):
            pages = pages.indices(pdfr.getNumPages())
        elif not isinstance(pages, tuple):
            raise TypeError('"pages" must be a tuple of (start, stop[, step])')

        srcpages = []
        if bookmark:
            bookmark = Bookmark(TextStringObject(bookmark),
                                NumberObject(self.id_count),
                                NameObject('/Fit'))

        outline = []
        if import_bookmarks:
            outline = pdfr.getOutlines()
            outline = self._trim_outline(pdfr, outline, pages)

        if bookmark:
            self.bookmarks += [bookmark, outline]
        else:
            self.bookmarks += outline

        dests = pdfr.namedDestinations
        dests = self._trim_dests(pdfr, dests, pages)
        self.named_dests += dests

        # Gather all the pages that are going to be merged
        for i in range(*pages):
            pg = pdfr.getPage(i)

            id = self.id_count
            self.id_count += 1

            mp = _MergedPage(pg, pdfr, id)

            srcpages.append(mp)

        self._associate_dests_to_pages(srcpages)
        self._associate_bookmarks_to_pages(srcpages)

        # Slice to insert the pages at the specified position
        self.pages[position:position] = srcpages

        # Keep track of our input files so we can close them later
        self.inputs.append((fileobj, pdfr, my_file))
예제 #26
0
파일: export_pdf.py 프로젝트: Ciwara/GCiss
def pdf_view(invoice):
    """
        cette views est cree pour la generation du PDF
    """
    # print(filename)
    # on recupere les items de la facture
    items_invoice = invoice.get("data")
    file_name = invoice.get("file_name")
    # title = invoice.get("title")

    # Static source pdf to be overlayed
    template_pdf = 'templates/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(template_pdf, "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(50, 683, invoice.get("invoice_type") +
                     " N° : " + str(invoice.get("number")))
        p.drawString(50, 667, "Doit à :" +
                     str(invoice.get("name_client")))
        p.drawString(450, 683, "Date : " + str(invoice.get("invoice_date")))
        # On ecrit les invoiceitem
        x, y = 20, 625
        cpt = 1
        ht = 0
        print(items_invoice)
        for qtty, designation, amount, montant in items_invoice:
            # montant = qtty * amount
            p.drawString(x + 43, y, str(cpt))
            p.drawString(x + 55, y, str(designation))
            p.drawRightString(
                x + 378, y, str(formatted_number(qtty)))
            p.drawRightString(
                x + 452, y, str(formatted_number(amount)))
            p.drawRightString(
                x + 535, y, str(formatted_number(montant)))
            y -= 20
            ht += montant
            cpt += 1

        ht_en_lettre = num2words(ht)
        p.drawRightString(555, 223, str(formatted_number(ht)))
        ht_en_lettre1, ht_en_lettre2 = controle_caratere(
            "Arrêté la présente facture à la somme de : " + ht_en_lettre +
            " francs CFA", 10, 90)
        p.drawString(50, 176, (ht_en_lettre1))
        p.drawString(55, 160, (ht_en_lettre2))
        p.drawString(450, 120, ('Le 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)
    file_dest = file_name
    output_stream = file(file_dest, u"wb")
    output.write(output_stream)
    output_stream.close()

    return file_dest
예제 #27
0
파일: configure.py 프로젝트: Elef-PING/Elef
def setConfigure(configure=dict):
    f = file(CONFIGURE_PATH, "w")
    json.dump(configure, f)
예제 #28
0
파일: export_pdf.py 프로젝트: fadiga/mstock
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
예제 #29
0
def reduceFile(files, compress=True):
    """
        input:
        fp: a list of file to reduce
        return:
        keepPage: pages to keep
    """
    newfiles = []
    for fp in files:
        print("Reducing file \"{}\"...".format(fp))
        # check if file can be opened
        try:
            # read pdf with pypdf2
            PDF = PdfFileReader(file(fp, 'rb'))
        except IOError as e:
            print("I/O error: Please input an existing pdf file:", fp)
            raise
        except:
            print(fp, ": Unexpected error - ", sys.exc_info()[0])
            raise
        output = PdfFileWriter()

        # keepPage is the page to be outputed
        keepPage = []
        idx = PDF.getNumPages() - 1
        prev = ""
        for i in range(PDF.getNumPages() - 1, -1, -1):
            page = PDF.getPage(i)
            curr = page.extractText()
            if not curr:
                if idx > i:
                    keepPage.append(idx)
                keepPage.append(i)
                idx = i - 1
                page = PDF.getPage(idx)
                prev = page.extractText()
                continue
            # set the page length of last idxed page
            if len(curr) > len(prev):
                length = len(prev)
            else:
                length = len(curr)
            # calculate the similarity btw last page and curr page
            seq = difflib.SequenceMatcher(lambda x: x == " ", curr[0:length],
                                          prev[0:length])
            ratio = seq.ratio()
            # print "page is {}, ratio is {}".format(i, ratio)
            # decision making, update index
            if ratio < 0.8:
                keepPage.append(idx)
                idx = i
            prev = curr
        if 0 not in keepPage:
            keepPage.append(0)
        # print keepPage

        # backward idx to forward idx
        keepPage.reverse()
        # add page numbers to output
        for i in keepPage:
            p = PDF.getPage(i)
            output.addPage(p)

        # TODO: need to change output path later
        # rename file
        newfile = fp[0:-4] + "_reduced.pdf"
        # add file into returned filename list
        newfiles.append(newfile)
        # write file into pdf
        with open(newfile, 'wb') as f:
            output.write(f)

    # compress file into a tar file
    if compress:
        with tarfile.open("reduced_files.tar", "w") as tar:
            for name in newfiles:
                tar.add(name)
    return newfiles
예제 #30
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