Example #1
0
def view(pdf_name):

    if request.method == "POST":
        req = request.values
        x = int(req["x"])
        y = int(req["y"])

        pdf = FPDF()
        pdf.add_page()
        pdf.set_font("Arial", size=12)
        pdf.cell(x)  #left align settting
        pdf.cell(x, y, txt="sample text", ln=1, align="C")
        pdf.output("Watermark_pdf/water.pdf")

        input_file = "Orignal_pdf/" + pdf_name
        watermark_file = "Watermark_pdf/water.pdf"
        #create the sample pdf for insert the data into your pdf
        output_file = "static/pdf/sample.pdf"

        with open(input_file, "rb") as filehandle_input:
            pdf = PyPDF2.PdfFileReader(filehandle_input)
            with open(watermark_file, "rb") as filehandle_watermark:
                watermark = PyPDF2.PdfFileReader(filehandle_watermark)
                total_input = pdf.getNumPages()
                first_page_watermark = watermark.getPage(0)
                for i in range(0, total_input):
                    pdf.getPage(i).mergePage(first_page_watermark)

                pdf_writer = PyPDF2.PdfFileWriter()

                for i in range(0, total_input):
                    pdf_writer.addPage(pdf.getPage(i))

                with open(output_file, "wb") as filehandle_output:
                    pdf_writer.write(filehandle_output)
        return render_template("pdf.html", pdf_name=pdf_name, x=x, y=y)
    return render_template("pdf.html")
linePDF.add_page()
linePDF.set_fill_color(0, 0, 0)
linePDF.rect(34, 281.5, 156, 0.5, 'F')
linePDF.output('component/line.pdf', 'F')

## Add lines to every page
outputPDF = PdfFileWriter()
sourcePDF = PdfFileReader(open('./pdf/' + filename, "rb"))

# print how many pages sourcePDF has:
# print (sourcePDF.numPages)
linePDF = PdfFileReader(open("component/line.pdf", "rb"))

for pageNum in range(sourcePDF.numPages):
    if pageNum == 0:
        linePlace = linePDF.getPage(0)
    elif pageNum == 2:
        linePlace = linePDF.getPage(1)
    else:
        linePlace = linePDF.getPage(2)

    if pageNum == 0 or pageNum >= 2:
        currentPage = sourcePDF.getPage(pageNum)
        currentPage.mergePage(linePlace)
        outputPDF.addPage(currentPage)
## finally, write "outputPDF" to document-outputPDF.pdf
outputPDFStream = open('./pdf/processed_latest.pdf', "wb")
outputPDF.write(outputPDFStream)

print('PDF preprocess finished')
linePDF.rect(103.5, 122, 0.25, 79, 'F')

linePDF.set_fill_color(0, 0, 0)
linePDF.rect(18.5, 206, 51, 0.25, 'F')
linePDF.set_fill_color(0, 0, 0)
linePDF.rect(18.5, 212, 51, 0.25, 'F')

linePDF.output('component/line_summary_table.pdf', 'F')

# --- Add lines to every page
outputPDF = PdfFileWriter()
sourcePDF = PdfFileReader(open('./pdf/' + filename, "rb"))
linePDF = PdfFileReader(open("component/line_summary_table.pdf", "rb"))

page0 = sourcePDF.getPage(0)
page0.mergePage(linePDF.getPage(0))
outputPDF.addPage(page0)
## --- finally, write "outputPDF" to document-outputPDF.pdf
outputPDFStream = open('./pdf/processed_summary.pdf', "wb")
outputPDF.write(outputPDFStream)

print('PDF summary preprocess finished')

# --- Start to parse the PDF
# filename = '74_1690.pdf'

pdf = pdfplumber.open('./pdf/processed_summary_copy.pdf')

page0 = pdf.pages[0]
bounding_box = (50, 330, 300, 610)
page_crop = page0.within_bbox(bounding_box)
Example #4
0
pdfFileObj = uploaded_file
pdfReader = PyPDF4.PdfFileReader(pdfFileObj)

totalpaginas = pdfReader.getNumPages()

#############

WIDTH = 210
HEIGHT = 297
pdf_background = FPDF()
pdf_background.add_page()
pdf_background.image("rect1.png", 10, 280, WIDTH - 50, HEIGHT - 100)
pdf_background.output("rect.pdf", "F")
pdf_background = PdfFileReader(open("rect.pdf", "rb"))
pdf_background.getPage(0)

for i in range(totalpaginas):

    pageObj = pdfReader.getPage(i)
    teste = pageObj.extractText().split()
    contador1 = 0
    contador2 = 0

    for j in teste:
        if j == 'dinheiro.':
            p = pdfReader.getPage(i)
            #pdf_writer.addPage(p)
            #pdf_writer.getPage(0)
            page = pdf_background.getPage(0)
            page.mergePage(p)
Example #5
0
def name(pdf_name):

    sample_list = []
    if request.method == "POST" and request.files:
        req = request.values
        x = int(req["x"])
        y = int(req["y"])
        zipname = req["zipname"]
        csvfile = request.files["csvfile"]
        csv_name = csvfile.filename
        csvfile.save(
            os.path.join(app.config["IMAGE_UPLOADS"] + "/CSV_Files", csv_name))
        print("upload success")

        with open('CSV_Files/' + csv_name, mode='r') as name:
            csv_reader = csv.DictReader(name)
            csv_reader = list(csv_reader)
            print(len(csv_reader))
            print(type(csv_reader))
            count = 0
            #create pdf for all names in csv file
            for row in pbar(csv_reader):
                pdf = FPDF()
                pdf.add_page()
                pdf.set_font("Arial", size=12)
                pdf.cell(x)  #left align settting
                pdf.cell(x, y, txt=" ", ln=1, align="C")
                pdf.output("Watermark_pdf/" + row["name"] + ".pdf")

                input_file = "Orignal_pdf/" + pdf_name
                watermark_file = "Watermark_pdf/" + row["name"] + ".pdf"

                mk_path = app.config["IMAGE_UPLOADS"] + "/Result_of_pdf"

                if row["name"] == 'LOT 1':
                    os.mkdir(mk_path)
                    os.mkdir(mk_path + "/lot1")
                    out = "Result_of_pdf/lot1/"
                elif row["name"] == 'LOT 2':
                    os.mkdir(mk_path + "/lot2")
                    out = "Result_of_pdf/lot2/"
                elif row["name"] == 'LOT 3':
                    os.mkdir(mk_path + "/lot3")
                    out = "Result_of_pdf/lot3/"
                elif row["name"] == 'LOT 4':
                    os.mkdir(mk_path + "/lot4")
                    out = "Result_of_pdf/lot4/"
                elif row["name"] == 'LOT 5':
                    os.mkdir(mk_path + "/lot5")
                    out = "Result_of_pdf/lot5/"
                elif row["name"] == 'LOT 6':
                    os.mkdir(mk_path + "/lot6")
                    out = "Result_of_pdf/lot6/"
                elif row["name"] == 'LOT 7':
                    os.mkdir(mk_path + "/lot7")
                    out = "Result_of_pdf/lot7/"

                name = row["name"]  #.replace(" ","_")
                if len(sample_list) == 0:
                    sample_list.append(name + ".pdf")

                output = out + name + ".pdf"
                output_file = output

                with open(input_file, "rb") as filehandle_input:
                    # read content of the original file
                    pdf = PyPDF2.PdfFileReader(filehandle_input)

                    with open(watermark_file, "rb") as filehandle_watermark:
                        # read content of the watermark
                        watermark = PyPDF2.PdfFileReader(filehandle_watermark)
                        # total_water = watermark.getNumPages()
                        total_input = pdf.getNumPages()

                        # get first page of the original PDF
                        first_page_watermark = watermark.getPage(0)

                        for i in range(0, total_input):
                            pdf.getPage(i).mergePage(first_page_watermark)

                        pdf_writer = PyPDF2.PdfFileWriter()

                        for i in range(0, total_input):
                            pdf_writer.addPage(pdf.getPage(i))

                        with open(output_file, "wb") as filehandle_output:
                            # write the watermarked file to the new file
                            pdf_writer.write(filehandle_output)

                count = int(count + 1)

        def zipdir(path, ziph):
            # ziph is zipfile handle
            for root, dirs, files in os.walk(path):
                for file in files:
                    ziph.write(os.path.join(root, file))

        zipf = zipfile.ZipFile(zipname + '.zip', 'w', zipfile.ZIP_DEFLATED)
        zipdir('Result_of_pdf/', zipf)
        zipf.close()
        sample = sample_list[0]
        print(sample)
        return render_template("download.html")
    return render_template("xy.html")