Example #1
0
def remove_pages(file_in, file_out, first_page, last_page):
    document = Poppler.Document.new_from_file('file://' + file_in, None)
    number_of_pages = document.get_n_pages()
    if first_page > number_of_pages - 1:
        first_page = number_of_pages - 1
    if last_page < first_page:
        last_page = first_page
    if last_page > number_of_pages - 1:
        last_page = number_of_pages - 1
    temp_pdf = tools.create_temp_file()
    pdfsurface = cairo.PDFSurface(temp_pdf, 200, 200)
    context = cairo.Context(pdfsurface)
    for i in range(0, number_of_pages):
        if i not in list(range(first_page, last_page + 1)):
            current_page = document.get_page(i)
            context.save()
            pdf_width, pdf_height = current_page.get_size()
            pdfsurface.set_size(pdf_width, pdf_height)
            current_page.render(context)
            context.restore()
            context.show_page()
    pdfsurface.flush()
    pdfsurface.finish()
    shutil.copy(temp_pdf, file_out)
    os.remove(temp_pdf)
Example #2
0
 def run(self):
     total_documents = len(self.pdfs_in)
     temp_pdf = tools.create_temp_file()
     pdfsurface = cairo.PDFSurface(temp_pdf, 200, 200)
     context = cairo.Context(pdfsurface)
     for index, file_in in enumerate(self.pdfs_in):
         self.emit('todo', file_in)
         document = Poppler.Document.new_from_file('file://' + file_in,
                                                   None)
         number_of_pages = document.get_n_pages()
         for i in range(0, number_of_pages):
             current_page = document.get_page(i)
             context.save()
             pdf_width, pdf_height = current_page.get_size()
             pdfsurface.set_size(pdf_width, pdf_height)
             current_page.render(context)
             context.restore()
             context.show_page()
             self.emit('donef',
                       (float(index) + float(i) / float(number_of_pages)) /
                       float(total_documents))
             if self.stop is True:
                 break
         if self.stop is True:
             break
     pdfsurface.flush()
     pdfsurface.finish()
     if os.path.exists(self.pdf_out):
         os.remove(self.pdf_out)
     shutil.copy(temp_pdf, self.pdf_out)
     os.remove(temp_pdf)
     if self.stop is True:
         self.emit('interrupted')
     else:
         self.emit('finished')
Example #3
0
 def run(self):
     pages = tools.get_pages_from_ranges(self.ranges)
     document = Poppler.Document.new_from_file('file://' + self.file_in,
                                               None)
     number_of_pages = document.get_n_pages()
     self.emit('start', number_of_pages)
     if number_of_pages > 0:
         temp_pdf = tools.create_temp_file()
         pdfsurface = cairo.PDFSurface(temp_pdf, 200, 200)
         context = cairo.Context(pdfsurface)
         self.emit('start', number_of_pages)
         for i in range(0, number_of_pages):
             self.emit('todo', '{0}/{1}'.format(i, number_of_pages))
             if i + 1 not in pages:
                 current_page = document.get_page(i)
                 context.save()
                 pdf_width, pdf_height = current_page.get_size()
                 pdfsurface.set_size(pdf_width, pdf_height)
                 current_page.render(context)
                 context.restore()
                 context.show_page()
             if self.stop is True:
                 break
             self.emit('done', '{0}/{1}'.format(i, number_of_pages))
         pdfsurface.flush()
         pdfsurface.finish()
         shutil.copy(temp_pdf, self.file_out)
         os.remove(temp_pdf)
     if self.stop is True:
         self.emit('interrupted')
     else:
         self.emit('finished')
Example #4
0
def remove_pages(file_in, file_out, first_page, last_page):
    document = Poppler.Document.new_from_file('file://' + file_in, None)
    number_of_pages = document.get_n_pages()
    if first_page > number_of_pages-1:
        first_page = number_of_pages-1
    if last_page < first_page:
        last_page = first_page
    if last_page > number_of_pages-1:
        last_page = number_of_pages-1
    temp_pdf = tools.create_temp_file()
    pdfsurface = cairo.PDFSurface(temp_pdf, 200, 200)
    context = cairo.Context(pdfsurface)
    for i in range(0, number_of_pages):
        if i not in list(range(first_page, last_page+1)):
            current_page = document.get_page(i)
            context.save()
            pdf_width, pdf_height = current_page.get_size()
            pdfsurface.set_size(pdf_width, pdf_height)
            current_page.render(context)
            context.restore()
            context.show_page()
    pdfsurface.flush()
    pdfsurface.finish()
    shutil.copy(temp_pdf, file_out)
    os.remove(temp_pdf)
Example #5
0
def rotate_and_flip_pages(file_pdf_in,
                          degrees=ROTATE_090,
                          flip_vertical=False,
                          flip_horizontal=False,
                          overwrite=False):
    temp_pdf = tools.create_temp_file()
    file_in = open(file_pdf_in, 'rb')
    file_out = open(temp_pdf, 'wb')
    reader = PyPDF2.PdfFileReader(file_in)
    writer = PyPDF2.PdfFileWriter()
    for i in range(0, reader.getNumPages()):
        page = reader.getPage(i)
        page.rotateClockwise(degrees)
        if flip_horizontal is True:
            page.scale(-1, 1)
        if flip_vertical is True:
            page.scale(1, -1)
        writer.addPage(page)
    writer.write(file_out)
    file_out.close()
    file_in.close()
    if overwrite:
        shutil.copy(temp_pdf, file_pdf_in)
    else:
        shutil.copy(
            temp_pdf,
            tools.get_output_filename(file_pdf_in,
                                      'rotated_' + str(int(degrees))))
    os.remove(temp_pdf)
Example #6
0
def encrypt(file_in, password):
    document_in = PdfFileReader(open(file_in, 'rb'))
    document_out = PdfFileWriter()
    document_out.cloneReaderDocumentRoot(document_in)
    document_out.encrypt(password)
    tmp_file = tools.create_temp_file()
    document_out.write(open(tmp_file, 'wb'))
    shutil.copy(tmp_file, file_in)
    os.remove(tmp_file)
Example #7
0
def rotate_ranges_in_pdf(file_in,
                         file_out,
                         degrees,
                         ranges,
                         flip_horizontal=False,
                         flip_vertical=False):
    pages = tools.get_pages_from_ranges(ranges)
    document = Poppler.Document.new_from_file('file://' + file_in, None)
    if document.get_n_pages() > 0:
        temp_pdf = tools.create_temp_file()
        pdfsurface = cairo.PDFSurface(temp_pdf, 200, 200)
        context = cairo.Context(pdfsurface)
        for i in range(0, document.get_n_pages()):
            current_page = document.get_page(i)
            if i + 1 in pages:
                if degrees == ROTATE_000 or degrees == ROTATE_180:
                    pdf_width, pdf_height = current_page.get_size()
                else:
                    pdf_height, pdf_width = current_page.get_size()
                pdfsurface.set_size(pdf_width, pdf_height)
                context.save()
                mtr = cairo.Matrix()
                mtr.rotate(degrees / 180.0 * math.pi)
                context.transform(mtr)
                if degrees == ROTATE_090:
                    context.translate(0.0, -pdf_width)
                elif degrees == ROTATE_180:
                    context.translate(-pdf_width, -pdf_height)
                elif degrees == ROTATE_270:
                    context.translate(-pdf_height, 0.0)
                if flip_vertical:
                    context.scale(1, -1)
                    if degrees == ROTATE_000 or degrees == ROTATE_180:
                        context.translate(0, -pdf_height)
                    else:
                        context.translate(0, -pdf_width)
                if flip_horizontal:
                    context.scale(-1, 1)
                    if degrees == ROTATE_000 or degrees == ROTATE_180:
                        context.translate(-pdf_width, 0)
                    else:
                        context.translate(-pdf_height, 0)
                current_page.render(context)
                context.restore()
            else:
                context.save()
                pdf_width, pdf_height = current_page.get_size()
                pdfsurface.set_size(pdf_width, pdf_height)
                current_page.render(context)
                context.restore()
            context.show_page()
        pdfsurface.flush()
        pdfsurface.finish()
        shutil.copy(temp_pdf, file_out)
        os.remove(temp_pdf)
Example #8
0
def add_paginate_all_pages(file_pdf_in,
                           color,
                           font,
                           size,
                           horizontal_position,
                           vertical_position,
                           horizontal_margin,
                           vertical_margin,
                           overwrite=False):
    document = Poppler.Document.new_from_file('file://' + file_pdf_in, None)
    number_of_pages = document.get_n_pages()
    if document.get_n_pages() > 0:
        temp_pdf = tools.create_temp_file()
        pdfsurface = cairo.PDFSurface(temp_pdf, 200, 200)
        context = cairo.Context(pdfsurface)
        for i in range(0, number_of_pages):
            current_page = document.get_page(i)
            text = '%s/%s' % (i + 1, number_of_pages)
            pdf_width, pdf_height = current_page.get_size()
            pdfsurface.set_size(pdf_width, pdf_height)
            context.save()
            current_page.render(context)
            context.restore()
            context.save()
            context.set_source_rgba(*color)
            context.select_font_face(font)
            context.set_font_size(size)
            xbearing, ybearing, font_width, font_height, xadvance, yadvance =\
                context.text_extents(text)
            if vertical_position == TOP:
                y = font_height + vertical_margin
            elif vertical_position == MIDLE:
                y = (pdf_height + font_height) / 2
            elif vertical_position == BOTTOM:
                y = pdf_height - vertical_margin
            if horizontal_position == LEFT:
                x = horizontal_margin
            elif horizontal_position == CENTER:
                x = (pdf_width - font_width) / 2
            elif horizontal_position == RIGHT:
                x = pdf_width - font_width + xbearing - horizontal_margin
            context.move_to(x, y)
            context.translate(x, y)
            context.show_text(text)
            context.restore()
            context.show_page()
        pdfsurface.flush()
        pdfsurface.finish()
        if overwrite:
            shutil.copy(temp_pdf, file_pdf_in)
        else:
            shutil.copy(temp_pdf,
                        tools.get_output_filename(file_pdf_in, 'paginated'))
        os.remove(temp_pdf)
Example #9
0
def add_watermark_to_all_pages(file_pdf_in,
                               file_image_in,
                               horizontal_position,
                               vertical_position,
                               horizontal_margin,
                               vertical_margin,
                               zoom,
                               overwrite=False):
    document = Poppler.Document.new_from_file('file://' + file_pdf_in, None)
    if document.get_n_pages() > 0:
        temp_pdf = tools.create_temp_file()
        watermark_surface = tools.create_image_surface_from_file(
            file_image_in, zoom)
        watermark_width = watermark_surface.get_width()
        watermark_height = watermark_surface.get_height()
        pdfsurface = cairo.PDFSurface(temp_pdf, 200, 200)
        context = cairo.Context(pdfsurface)
        for i in range(0, document.get_n_pages()):
            current_page = document.get_page(i)
            pdf_width, pdf_height = current_page.get_size()
            pdfsurface.set_size(pdf_width, pdf_height)
            context.save()
            current_page.render(context)
            context.restore()
            context.save()
            if vertical_position == TOP:
                y = vertical_margin
            elif vertical_position == MIDLE:
                y = (pdf_height - watermark_height / MMTOPIXEL) / 2
            elif vertical_position == BOTTOM:
                y = pdf_height - watermark_height / MMTOPIXEL - vertical_margin
            if horizontal_position == LEFT:
                x = horizontal_margin
            elif horizontal_position == CENTER:
                x = (pdf_width - watermark_width / MMTOPIXEL) / 2
            elif horizontal_position == RIGHT:
                x = pdf_width - watermark_width / MMTOPIXEL - horizontal_margin
            context.translate(x, y)
            context.scale(1.0 / MMTOPIXEL, 1.0 / MMTOPIXEL)
            context.set_source_surface(watermark_surface)
            context.paint()
            context.restore()
            context.show_page()
        pdfsurface.flush()
        pdfsurface.finish()
        if overwrite:
            shutil.copy(temp_pdf, file_pdf_in)
        else:
            shutil.copy(temp_pdf,
                        tools.get_output_filename(file_pdf_in, 'watermarked'))
        os.remove(temp_pdf)
Example #10
0
def rotate_ranges_in_pdf(file_in, file_out, degrees, ranges,
                         flip_horizontal=False, flip_vertical=False):
    pages = tools.get_pages_from_ranges(ranges)
    document = Poppler.Document.new_from_file('file://' + file_in, None)
    if document.get_n_pages() > 0:
        temp_pdf = tools.create_temp_file()
        pdfsurface = cairo.PDFSurface(temp_pdf, 200, 200)
        context = cairo.Context(pdfsurface)
        for i in range(0, document.get_n_pages()):
            current_page = document.get_page(i)
            if i+1 in pages:
                if degrees == ROTATE_000 or degrees == ROTATE_180:
                    pdf_width, pdf_height = current_page.get_size()
                else:
                    pdf_height, pdf_width = current_page.get_size()
                pdfsurface.set_size(pdf_width, pdf_height)
                context.save()
                mtr = cairo.Matrix()
                mtr.rotate(degrees/180.0*math.pi)
                context.transform(mtr)
                if degrees == ROTATE_090:
                        context.translate(0.0, -pdf_width)
                elif degrees == ROTATE_180:
                        context.translate(-pdf_width, -pdf_height)
                elif degrees == ROTATE_270:
                        context.translate(-pdf_height, 0.0)
                if flip_vertical:
                    context.scale(1, -1)
                    if degrees == ROTATE_000 or degrees == ROTATE_180:
                        context.translate(0, -pdf_height)
                    else:
                        context.translate(0, -pdf_width)
                if flip_horizontal:
                    context.scale(-1, 1)
                    if degrees == ROTATE_000 or degrees == ROTATE_180:
                        context.translate(-pdf_width, 0)
                    else:
                        context.translate(-pdf_height, 0)
                current_page.render(context)
                context.restore()
            else:
                context.save()
                pdf_width, pdf_height = current_page.get_size()
                pdfsurface.set_size(pdf_width, pdf_height)
                current_page.render(context)
                context.restore()
            context.show_page()
        pdfsurface.flush()
        pdfsurface.finish()
        shutil.copy(temp_pdf, file_out)
        os.remove(temp_pdf)
Example #11
0
def decrypt(file_in, password):
    document_in = PdfFileReader(open(file_in, 'rb'))
    if document_in.isEncrypted:
        while True:
            matched = document_in.decrypt(password)
            if matched:
                document_out = PdfFileWriter()
                document_out.cloneReaderDocumentRoot(document_in)
                tmp_file = tools.create_temp_file()
                document_out.write(open(tmp_file, 'wb'))
                shutil.copy(tmp_file, file_in)
                os.remove(tmp_file)
                return True
    return False
Example #12
0
def add_paginate_all_pages(file_pdf_in, color, font, size, horizontal_position,
                           vertical_position, horizontal_margin,
                           vertical_margin, overwrite=False):
    document = Poppler.Document.new_from_file('file://' + file_pdf_in, None)
    number_of_pages = document.get_n_pages()
    if document.get_n_pages() > 0:
        temp_pdf = tools.create_temp_file()
        pdfsurface = cairo.PDFSurface(temp_pdf, 200, 200)
        context = cairo.Context(pdfsurface)
        for i in range(0, number_of_pages):
            current_page = document.get_page(i)
            text = '%s/%s' % (i+1, number_of_pages)
            pdf_width, pdf_height = current_page.get_size()
            pdfsurface.set_size(pdf_width, pdf_height)
            context.save()
            current_page.render(context)
            context.restore()
            context.save()
            context.set_source_rgba(*color)
            context.select_font_face(font)
            context.set_font_size(size)
            xbearing, ybearing, font_width, font_height, xadvance, yadvance =\
                context.text_extents(text)
            if vertical_position == TOP:
                y = font_height + vertical_margin
            elif vertical_position == MIDLE:
                y = (pdf_height + font_height)/2
            elif vertical_position == BOTTOM:
                y = pdf_height - vertical_margin
            if horizontal_position == LEFT:
                x = horizontal_margin
            elif horizontal_position == CENTER:
                x = (pdf_width - font_width)/2
            elif horizontal_position == RIGHT:
                x = pdf_width - font_width + xbearing - horizontal_margin
            context.move_to(x, y)
            context.translate(x, y)
            context.show_text(text)
            context.restore()
            context.show_page()
        pdfsurface.flush()
        pdfsurface.finish()
        if overwrite:
            shutil.copy(temp_pdf, file_pdf_in)
        else:
            shutil.copy(temp_pdf, tools.get_output_filename(
                file_pdf_in, 'paginated'))
        os.remove(temp_pdf)
Example #13
0
def add_watermark_to_all_pages(file_pdf_in, file_image_in, horizontal_position,
                               vertical_position, horizontal_margin,
                               vertical_margin, zoom, overwrite=False):
    document = Poppler.Document.new_from_file('file://' + file_pdf_in, None)
    if document.get_n_pages() > 0:
        temp_pdf = tools.create_temp_file()
        watermark_surface = tools.create_image_surface_from_file(
            file_image_in, zoom)
        watermark_width = watermark_surface.get_width()
        watermark_height = watermark_surface.get_height()
        pdfsurface = cairo.PDFSurface(temp_pdf, 200, 200)
        context = cairo.Context(pdfsurface)
        for i in range(0, document.get_n_pages()):
            current_page = document.get_page(i)
            pdf_width, pdf_height = current_page.get_size()
            pdfsurface.set_size(pdf_width, pdf_height)
            context.save()
            current_page.render(context)
            context.restore()
            context.save()
            if vertical_position == TOP:
                y = vertical_margin
            elif vertical_position == MIDLE:
                y = (pdf_height - watermark_height/MMTOPIXEL)/2
            elif vertical_position == BOTTOM:
                y = pdf_height - watermark_height/MMTOPIXEL - vertical_margin
            if horizontal_position == LEFT:
                x = horizontal_margin
            elif horizontal_position == CENTER:
                x = (pdf_width - watermark_width/MMTOPIXEL)/2
            elif horizontal_position == RIGHT:
                x = pdf_width - watermark_width/MMTOPIXEL - horizontal_margin
            context.translate(x, y)
            context.scale(1.0/MMTOPIXEL, 1.0/MMTOPIXEL)
            context.set_source_surface(watermark_surface)
            context.paint()
            context.restore()
            context.show_page()
        pdfsurface.flush()
        pdfsurface.finish()
        if overwrite:
            shutil.copy(temp_pdf, file_pdf_in)
        else:
            shutil.copy(temp_pdf, tools.get_output_filename(
                file_pdf_in, 'watermarked'))
        os.remove(temp_pdf)
Example #14
0
def join_files(files, file_out):
    temp_pdf = tools.create_temp_file()
    pdfsurface = cairo.PDFSurface(temp_pdf, 200, 200)
    context = cairo.Context(pdfsurface)
    for file_in in files:
        document = Poppler.Document.new_from_file('file://' + file_in, None)
        number_of_pages = document.get_n_pages()
        for i in range(0, number_of_pages):
            current_page = document.get_page(i)
            context.save()
            pdf_width, pdf_height = current_page.get_size()
            pdfsurface.set_size(pdf_width, pdf_height)
            current_page.render(context)
            context.restore()
            context.show_page()
    pdfsurface.flush()
    pdfsurface.finish()
    shutil.copy(temp_pdf, file_out)
    os.remove(temp_pdf)
Example #15
0
 def run(self):
     temp_pdf = tools.create_temp_file()
     pdfsurface = cairo.PDFSurface(temp_pdf, self.width, self.height)
     context = cairo.Context(pdfsurface)
     self.emit('start', self.files)
     for image in self.files:
         self.emit('todo', image)
         if mimetypes.guess_type(image)[0] in MIMETYPES_PNG:
             imagesurface = cairo.ImageSurface.create_from_png(image)
         else:
             imagesurface = tools.create_image_surface_from_file(image)
         imagesurface_width = imagesurface.get_width()
         imagesurface_height = imagesurface.get_height()
         scale_x = (imagesurface_width / MMTOPIXEL) / self.width
         scale_y = (imagesurface_height / MMTOPIXEL) / self.height
         if scale_x > scale_y:
             scale = scale_x
         else:
             scale = scale_y
         if self.margin == 1:
             scale = scale * 1.05
         elif self.margin == 2:
             scale = scale * 1.15
         x = (self.width - imagesurface_width / MMTOPIXEL / scale) / 2
         y = (self.height - imagesurface_height / MMTOPIXEL / scale) / 2
         context.save()
         context.translate(x, y)
         context.scale(1.0 / MMTOPIXEL / scale, 1.0 / MMTOPIXEL / scale)
         context.set_source_surface(imagesurface)
         context.paint()
         context.restore()
         context.show_page()
         self.emit('done', image)
         if self.stop is True:
             break
     pdfsurface.flush()
     pdfsurface.finish()
     shutil.copy(temp_pdf, self.file_out)
     os.remove(temp_pdf)
     if self.stop is True:
         self.emit('interrupted')
     else:
         self.emit('finished')
Example #16
0
def join_files(files, file_out):
    temp_pdf = tools.create_temp_file()
    pdfsurface = cairo.PDFSurface(temp_pdf, 200, 200)
    context = cairo.Context(pdfsurface)
    for file_in in files:
        document = Poppler.Document.new_from_file('file://' + file_in, None)
        number_of_pages = document.get_n_pages()
        for i in range(0, number_of_pages):
            current_page = document.get_page(i)
            context.save()
            pdf_width, pdf_height = current_page.get_size()
            pdfsurface.set_size(pdf_width, pdf_height)
            current_page.render(context)
            context.restore()
            context.show_page()
    pdfsurface.flush()
    pdfsurface.finish()
    shutil.copy(temp_pdf, file_out)
    os.remove(temp_pdf)
Example #17
0
 def run(self):
     total_documents = len(self.files)
     pages = tools.get_pages_from_ranges(self.ranges)
     if total_documents > 0 and pages:
         for index, file_in in enumerate(self.files):
             self.emit('todo', file_in)
             document = Poppler.Document.new_from_file(
                 'file://' + file_in, None)
             number_of_pages = document.get_n_pages()
             if number_of_pages > 1:
                 filename, _ = os.path.splitext(file_in)
                 file_out = filename + '_extracted_pages.pdf'
                 temp_pdf = tools.create_temp_file()
                 pdfsurface = cairo.PDFSurface(temp_pdf, 200, 200)
                 context = cairo.Context(pdfsurface)
                 for i in range(0, number_of_pages):
                     if i + 1 in pages:
                         current_page = document.get_page(i)
                         context.save()
                         pdf_width, pdf_height = current_page.get_size()
                         pdfsurface.set_size(pdf_width, pdf_height)
                         current_page.render(context)
                         context.restore()
                         context.show_page()
                     self.emit(
                         'donef',
                         (float(index) + float(i) / float(number_of_pages))
                         / float(total_documents))
                     if self.stop is True:
                         break
                 pdfsurface.flush()
                 pdfsurface.finish()
                 shutil.copy(temp_pdf, file_out)
                 os.remove(temp_pdf)
             if self.stop is True:
                 break
     if self.stop is True:
         self.emit('interrupted')
     else:
         self.emit('finished')