Ejemplo n.º 1
0
    def __init__(self,
                 file_name,
                 rotation,
                 suffix='rotated',
                 tempdir=None,
                 method='pdfrw'):
        self.file_name = file_name
        self.rotation = rotation
        self.suffix = suffix
        self.tempdir = tempdir

        if tempdir:
            with NamedTemporaryFile(suffix='.pdf', dir=tempdir,
                                    delete=False) as temp:
                self.outfn = temp.name
        elif suffix:
            self.outfn = os.path.join(os.path.dirname(file_name),
                                      add_suffix(file_name, suffix))
        else:
            self.outfn = NamedTemporaryFile(suffix='.pdf').name

        if method == 'pypdf3':
            self.pypdf3()
        else:
            self.pdfrw()
Ejemplo n.º 2
0
 def _get_output(self, index):
     if self.output:
         return self.output
     elif not self.tempdir:
         output_file = add_suffix(self.file_name, str(index + 1), ext=self.ext)
         return os.path.join(self.output_dir, output_file)
     else:
         with NamedTemporaryFile(suffix=self.ext, dir=self.tempdir, delete=True) as temp:
             return temp.name
Ejemplo n.º 3
0
 def save(self, remove_temps=True):
     if self.imgs is None:
         self.get_imgs()
     i2p = IMG2PDF(self.imgs, self.directory, self.tempdir,
                   self.progress_bar)
     self.pdf = i2p.save(clean_temp=False,
                         output_name=add_suffix(self._file_name,
                                                self.suffix))
     self.cleanup(remove_temps)
     return self.pdf
Ejemplo n.º 4
0
def slicer(document,
           first_page=None,
           last_page=None,
           suffix='sliced',
           tempdir=None):
    """Slice a PDF document to remove pages."""
    # Set output file name
    if tempdir:
        with NamedTemporaryFile(suffix='.pdf', dir=tempdir,
                                delete=False) as temp:
            output = temp.name
    elif suffix:
        output = os.path.join(os.path.dirname(document),
                              add_suffix(document, suffix))
    else:
        with NamedTemporaryFile(suffix='.pdf') as temp:
            output = temp.name

    # Reindex page selections for simple user input
    first_page = first_page - 1 if not None else None

    # Validate page range by comparing selection to number of pages in PDF document
    pages = Info(document).pages
    invalid = 'Number of pages: ' + str(
        pages) + ' ----> Page Range Input: ' + str(first_page) + '-' + str(
            last_page)
    assert first_page <= last_page <= pages, invalid

    pdf = PdfFileReader(document)
    writer = PdfFileWriter()

    pages = list(range(pdf.getNumPages()))[first_page:last_page]
    for page in pages:
        writer.addPage(pdf.getPage(page))

    with open(output, 'wb') as out:
        writer.write(out)
    return output
Ejemplo n.º 5
0
    def __init__(self,
                 file_name,
                 margin_x=0,
                 margin_y=0,
                 scale=1.5,
                 suffix='scaled',
                 tempdir=None,
                 method='pdfrw'):
        self.file_name = file_name
        self.margin_x = margin_x
        self.margin_y = margin_y
        self.scale = scale
        self.suffix = suffix

        # Set output file name
        if tempdir:
            with NamedTemporaryFile(suffix='_' + suffix + '.pdf',
                                    dir=tempdir,
                                    delete=False) as temp:
                self.output = temp.name
        elif suffix:
            self.output = os.path.join(os.path.dirname(file_name),
                                       add_suffix(file_name, suffix))
        else:
            self.output = NamedTemporaryFile(suffix='_' + suffix + '.pdf').name

        # Get target width and height
        dims = Info(self.file_name).dimensions
        self.target_w = dims['w'] * self.scale
        self.target_h = dims['h'] * self.scale

        # Execute either pdfrw or PyPDF3 method
        if method == 'pypdf3':
            self.pypdf3()
        else:
            self.pdfrw()