def fill_form_csvdata(pdf_file, csv_file, fields2fill): pdfForm = str(pdf_file) pdfjinja = PdfJinja(pdfForm) filled_forms_path = relative_project_path('files') + "/" counter = 0 with open(str(csv_file), 'rU') as csvfile: csv_data = csv.reader(csvfile, delimiter=';') len_fields2fill = str(len(fields2fill)) len_csv_columns = str(len(next(csv_data))) if len_fields2fill is not len_csv_columns: return "Columns error" csvfile.seek(0) for row in csv_data: #try: dict_temp = {} for x, i in enumerate(fields2fill): dict_temp[str(i)] = row[int(x)].decode('utf-8') pdfout = pdfjinja(dict_temp) pdfResult = filled_forms_path + "filled_form_" + str(counter) + ".pdf" counter += 1 pdfout.write(open(pdfResult, 'wb')) #except: # return "Filling error" return filled_forms_path
def fill_pdf(pdf_template, fields_to_fill, outfile): """Populates a PDF template with fields specified by user and writes the filled PDF to disk. Field names should be defined in the pdf_template. Parameters ---------- pdf_template : str, path to file path to the template PDF fields_to_fill : dict fields to populate and the values to populate them with outfile : str, path to file path to the PDF output file that will be generated Returns ------- outfile : str if output file successfully created, returns path to that file Raises ------ FileNotFoundError if output file is not generated """ template = PdfJinja(pdf_template) filled = template(fields_to_fill) filled.write(open(outfile, 'wb')) if os.path.exists(outfile): return outfile else: raise FileNotFoundError('Failed to produce output file.')
def res_positif(self, fullpath, type_dt_dict, noConsultationDuTeleservice, multi_poly_proj, union_line, count_intersection, epsg, min_poly_x, min_poly_y, max_poly_x, max_poly_y): try: name_reseau = self.name_reseau path_output = os.path.join(os.path.abspath(os.path.dirname(__file__)), "output" + os.sep) path_model_cerfa = os.path.join(os.path.abspath(os.path.dirname(__file__)), "cerfa_modele" + os.sep + "cerfa_14435-03.pdf") self.vider_dossier() # creation du grid self.grid(min_poly_x, min_poly_y, max_poly_x, max_poly_y) # enveloppe avec un buffer sur le grid env_grid = self.grid_enveloppe() # calcul de l'echelle obtenu a partir de l'enveloppe du grid et son buffer scale = self.mapnik_config(env_grid[0], env_grid[1], env_grid[2], env_grid[3]) pdfjinja = PdfJinja(path_model_cerfa) pdfout = pdfjinja(dict(NoGU=noConsultationDuTeleservice, Echelle1=scale[2])) pdfout.write(open(path_output + 'cerfa_' + noConsultationDuTeleservice + '.pdf', 'wb')) self.mapnik_output(env_grid[0], env_grid[1], env_grid[2], env_grid[3], epsg, multi_poly_proj, union_line, noConsultationDuTeleservice) print(name_reseau, type_dt_dict, noConsultationDuTeleservice, multi_poly_proj, union_line, count_intersection) os.remove(fullpath) except: print('erreur : objet output, function res +')
def pdflsk(request, projekid): dataobject = document.Laporansiapkerja.objects.filter( projekbind=projekid).first() idstr = str(dataobject.projekbind.id) print(idstr) pdfjinja = PdfJinja('static_in_env/assets/pdf/Laporansiapkerja.pdf') pdfout = pdfjinja( dict( kontraktor=dataobject.lskmrksatulink.mrksatukontraktor.konNama, alamat1=dataobject.lskmrksatulink.mrksatukontraktor.konAlamat, alamat2=dataobject.lskmrksatulink.mrksatukontraktor.konAlamatExtS, poskod=dataobject.lskmrksatulink.mrksatukontraktor.konPoskod, bandar=dataobject.lskmrksatulink.mrksatukontraktor.konBandar, negeri=dataobject.lskmrksatulink.mrksatukontraktor.konNegeri, kerja=dataobject.projekbind.tajukkerja, inden=dataobject.lskmrksatulink.mrksatunoinden, sebutharga=dataobject.projekbind.nosebuthargaid, workmen=dataobject.lskperkeso, ins1=dataobject.lsknoinsurancesatu, ins2=dataobject.lsknoinsurancedua, peruntukan=dataobject.lskperuntukan, kosprojek=intcomma(dataobject.lskmrksatulink.mrksatukosprojek), hargasebenar=intcomma(dataobject.lskhargasebenar), tarikhmula=dataobject.lskmrksatulink.mrksatutarikhmula, tarikhtamat=dataobject.lskmrksatulink.mrksatutarikhjangkasiap, tarikhlanjutmasa=dataobject.lsklanjutmasa, tarikhsiapsem=dataobject.lskkerjasiap, laporan=dataobject.lsklaporan, tarikhakui=dataobject.lsktarikhperakui, penyelia=dataobject.projekbind.nosebuthargaid.pegawaiselia. first_name, jawatan=dataobject.projekbind.nosebuthargaid.pegawaiselia. userprofile.jawatan, kb=dataobject.lskketuabahagian, jawatankb=dataobject.lskjawatanketuabahagian, jurutera=dataobject.lskjuruteraj41, jawatanjurutera=dataobject.lskjawatanj41, jd=dataobject.lskjuruteradaerah, jawatanjd=dataobject.lskjawatanjuruteradaerah, )) pdfout.write( open( 'static_in_env/assets/pdf/outputpdf/Laporansiapkerja-' + idstr + '.pdf', 'wb')) try: return FileResponse(open( 'static_in_env/assets/pdf/outputpdf/Laporansiapkerja-' + idstr + '.pdf', 'rb'), content_type='application/pdf') except FileNotFoundError: raise Http404() return render(request, 'pages/printtest.html')
def setUp(self): pdffile = os.path.join(self.datadir, "sample.pdf") jsonfile = os.path.join(self.datadir, "sample.json") Attachment.font = "examples/open-sans/regular.ttf" self.data = json.loads(open(jsonfile).read()) self.attachments = [ Attachment(**kwargs) for kwargs in self.data.pop("attachments") ] self.pdfjinja = PdfJinja(pdffile)
def process_each_record(data, domains, pdftemplate): dir_name = os.path.dirname(__file__) pdf_doc = arcpy.mapping.PDFDocumentCreate(dir_name + "\\example_report.pdf") template_pdf_file = os.path.join(dir_name, pdftemplate) template_pdf = PdfJinja(template_pdf_file) for record in data: record_attributes = record["attributes"] adjust_domain_structure(domains, record_attributes) fill_out_form(record_attributes, dir_name, template_pdf, pdf_doc)
def pdfmrkdua(request, projekid): dataobject = document.MRKDua.objects.filter(projekbind=projekid).first() idstr = str(dataobject.projekbind.id) print(idstr) pdfjinja = PdfJinja('static_in_env/assets/pdf/MRK-02.pdf') pdfout = pdfjinja( dict( nopkk=dataobject.mrksatulink.mrksatukontraktor. sijilPPKNoPendaftaran, kontraktor=dataobject.mrksatulink.mrksatukontraktor.konNama, nokontrak=dataobject.projekbind.nosebuthargaid, noinden=dataobject.mrksatulink.mrksatunoinden, kosprojek=intcomma(dataobject.mrksatulink.mrksatukosprojek), tarikhmilik=dataobject.mrksatulink.mrksatutarikhmula, tarikhjangkasiap=dataobject.mrksatulink.mrksatutarikhjangkasiap, kemajuanprojek=dataobject.mrkduakerjajadual, kemajuankerjasebenar=dataobject.mrkduakerjasebenartarikh, sehingga=dataobject.mrkduakerjasebenar, nobayaran=dataobject.mrkduakemajuan, jumlahbayaran=intcomma(dataobject.mrkduabayarankemajuan), check1=dataobject.mrkduamodal, check2=dataobject.mkrduabahan, check3=dataobject.mrkduapekerja, check4=dataobject.mrkduatapak, check5=dataobject.mrkduacuaca, disebabkan=dataobject.mrkduadisebabkanoleh, lainlain=dataobject.mrkdualainlain, lanjutmasa=dataobject.mrkdualanjutmasa, lanjutdari=dataobject.mrkdualanjutdari, lanjuthingga=dataobject.mrkdualanjutsehingga, disebabkanoleh=dataobject.mrkduadisebabkan, lad=dataobject.mrkduaLAD, laddari=dataobject.mrkduaLADdari, ladhingga=dataobject.mrkduaLADSehingga, tarikhperakuan=dataobject.mrkduaperakuan, tarikhmansuh=dataobject.mrkduamansuh, )) pdfout.write( open('static_in_env/assets/pdf/outputpdf/MRK-02-' + idstr + '.pdf', 'wb')) try: return FileResponse(open( 'static_in_env/assets/pdf/outputpdf/MRK-02-' + idstr + '.pdf', 'rb'), content_type='application/pdf') except FileNotFoundError: raise Http404() return render(request, 'pages/printtest.html')
def pdfpsmk(request, projekid): dataobject = document.PSMK.objects.filter(projekbind=projekid).first() lskfetch = document.Laporansiapkerja.objects.filter( projekbind=projekid).first() idstr = str(dataobject.projekbind.id) print(idstr) pdfjinja = PdfJinja('static_in_env/assets/pdf/PSMK.pdf') pdfout = pdfjinja( dict( kontrator=dataobject.psmkmrksatulink.mrksatukontraktor.konNama, alamat1=dataobject.psmkmrksatulink.mrksatukontraktor.konAlamat, alamat2=dataobject.psmkmrksatulink.mrksatukontraktor.konAlamatExtS, poskod=dataobject.psmkmrksatulink.mrksatukontraktor.konPoskod, bandar=dataobject.psmkmrksatulink.mrksatukontraktor.konBandar, negeri=dataobject.psmkmrksatulink.mrksatukontraktor.konNegeri, gred=dataobject.psmkmrksatulink.mrksatugred, sebutharga=dataobject.projekbind.nosebuthargaid, tajuk=dataobject.projekbind.tajukkerja, tarikhgagal=lskfetch.lskkerjasiap, g1=dataobject.psmknojaminanbanka, hargaa=intcomma(dataobject.psmkhargajaminana), bakia=intcomma(dataobject.psmkbakiwangjaminana), g2=dataobject.psmknojaminanbankab, hargab=intcomma(dataobject.psmkhargajaminanb), wangb=intcomma(dataobject.psmkbakiwangjaminanb), kosbon=intcomma(dataobject.psmkkosbon), bakikosbon=intcomma(dataobject.psmkbakikos), pegawai=dataobject.projekbind.nosebuthargaid.pegawaiselia. first_name, jawatan=dataobject.projekbind.nosebuthargaid.pegawaiselia. userprofile.jawatan, )) pdfout.write( open('static_in_env/assets/pdf/outputpdf/PSMK-' + str(idstr) + '.pdf', 'wb')) try: return FileResponse(open( 'static_in_env/assets/pdf/outputpdf/PSMK-' + str(idstr) + '.pdf', 'rb'), content_type='application/pdf') except FileNotFoundError: raise Http404() return render(request, 'pages/printtest.html')
def remplir_template(dirname_template, pdf_template, dirname_output_file, pdf_output, dictionnaire): """ Fonction qui permet de remplir un pdf template :param dirname_template: chemin du fichier de template :param pdf_template: nom du fichier de template :param dirname_output_file: chemin des pdf généré :param pdf_output: nom du fichier pdf à générer :param dictionnaire: dictionnaire contenant le nom des textfields des pdf ainsi que leurs valeurs :return: """ template_pdf_file = os.path.join(dirname_template, pdf_template) template_pdf = PdfJinja(template_pdf_file) rendered_pdf = template_pdf(dictionnaire) output_file = os.path.join(dirname_output_file, pdf_output) rendered_pdf.write(open(output_file, 'wb'))
def some_pdf(request, projekid): dataobject = document.MRKSatu.objects.filter( mrksatunosebutharga=projekid).first() print("this is data object", dataobject.mrksatunoinden) pdfjinja = PdfJinja('static_in_env/assets/pdf/form.pdf') pdfout = pdfjinja( dict(firstname=dataobject.mrksatukontraktor.sijilPPKNoPendaftaran)) pdfout.write(open('static_in_env/assets/pdf/filled.pdf', 'wb')) try: return FileResponse(open('static_in_env/assets/pdf/filled.pdf', 'rb'), content_type='application/pdf') except FileNotFoundError: raise Http404() return render(request, 'pages/printtest.html')
def the_form(request): if request.method == 'GET': return render(request, 'index.html', {}) form = PdfForm(request.POST, request.FILES) if form.is_valid(): data = form.cleaned_data.copy() for city in ['sydney', 'melbourne', 'adelaide', 'perth']: data[city] = city in data['city'] mypdf = PdfJinja('home/static/home/sydjango3.pdf') pdfout = mypdf(data) b = BytesIO(b'') pdfout.write(b) b.seek(0) invoice = Invoice.objects.create() invoice.document.save('mypdf.pdf', ContentFile(b.read())) return JsonResponse({'redirect_url': invoice.document.url}) return render(request, 'index.html', {'form': form})
def pdfpsksatu(request, projekid): dataobject = document.PSK.objects.filter(projekbind=projekid).first() lskfetch = document.Laporansiapkerja.objects.filter( projekbind=projekid).first() mrkduaf = document.MRKDua.objects.filter(projekbind=projekid).first() idstr = str(dataobject.projekbind.id) print(idstr) pdfjinja = PdfJinja('static_in_env/assets/pdf/PSK01.pdf') pdfout = pdfjinja( dict( sebutharga=dataobject.projekbind.nosebuthargaid, kontraktor=dataobject.pskmrksatulink.mrksatukontraktor.konNama, alamat1=dataobject.pskmrksatulink.mrksatukontraktor.konAlamat, alamat2=dataobject.pskmrksatulink.mrksatukontraktor.konAlamatExtS, poskod=dataobject.pskmrksatulink.mrksatukontraktor.konPoskod, bandar=dataobject.pskmrksatulink.mrksatukontraktor.konBandar, negeri=dataobject.pskmrksatulink.mrksatukontraktor.konNegeri, gred=dataobject.pskmrksatulink.mrksatugred, tajukkerja=dataobject.projekbind.tajukkerja, hargasebenar=intcomma(lskfetch.lskhargasebenar), tarikhsebenar=lskfetch.lskkerjasiap, tarikhambil=dataobject.psktarikhambilmilik, mulacacat=dataobject.psktarikhmulatanggug, akhircacat=dataobject.psktarikhtamattanggung, jurutere=dataobject.projekbind.nosebuthargaid.pegawaiselia. first_name, )) pdfout.write( open('static_in_env/assets/pdf/outputpdf/PSK01-' + idstr + '.pdf', 'wb')) try: return FileResponse(open( 'static_in_env/assets/pdf/outputpdf/PSK01-' + idstr + '.pdf', 'rb'), content_type='application/pdf') except FileNotFoundError: raise Http404() return render(request, 'pages/printtest.html')
def ssemak(request, projekid): dataobject = document.SenaraiSemakan.objects.filter( projekbind=projekid).first() lskfetch = document.Laporansiapkerja.objects.filter( projekbind=projekid).first() mrkduaf = document.MRKDua.objects.filter(projekbind=projekid).first() idstr = str(dataobject.projekbind.id) print(idstr) pdfjinja = PdfJinja('static_in_env/assets/pdf/SS.pdf') pdfout = pdfjinja( dict(check1=dataobject.ssinden, check2=dataobject.sslsk, check3=dataobject.ssti, check4=dataobject.sssebutharga, check5=dataobject.sspt, check6=dataobject.ssjs, check7=dataobject.sskts, check8=dataobject.ssds, check9=dataobject.ssplm, check10=dataobject.ssab, check11=dataobject.sscidb, check12=dataobject.sspkk, check13=dataobject.ssssm, check14=dataobject.sskk, check15=dataobject.ssinsurance, check16=dataobject.ssgambar, pegawai=dataobject.projekbind.nosebuthargaid.pegawaiselia. first_name, tarikh=dataobject.sstarikh)) pdfout.write( open('static_in_env/assets/pdf/outputpdf/SS-' + idstr + '.pdf', 'wb')) try: return FileResponse(open( 'static_in_env/assets/pdf/outputpdf/SS-' + idstr + '.pdf', 'rb'), content_type='application/pdf') except FileNotFoundError: raise Http404() return render(request, 'pages/printtest.html')
def setUp(self): pdffile = os.path.join(self.datadir, "sample.pdf") self.data = { 'firstName': 'Renan', 'lastName': 'Husson', 'address': { 'street': '24 rue de la pommes', 'apt': 'C317', 'city': 'TOULOUSE', 'zipcode': 31000 }, 'universite': 'Jean Jaures', 'spirit': 'Panda', 'evil': True, 'language': { 'french': True, 'esperento': True } } self.pdfjinja = PdfJinja(pdffile)
def pdfmrksatu(request, projekid): dataobject = document.MRKSatu.objects.filter(projekbind=projekid).first() idstr = str(dataobject.projekbind.id) print(idstr) pdfjinja = PdfJinja('static_in_env/assets/pdf/MRK-01.pdf') pdfout = pdfjinja( dict( sijilpkk=dataobject.mrksatukontraktor.sijilPPKNoPendaftaran, kontraktor=dataobject.mrksatukontraktor.konNama, nokontrak=dataobject.projekbind.nosebuthargaid, noinden=dataobject.mrksatunoinden, daereah=dataobject.mrksatukontraktor.konKawOperasi, negeri=dataobject.mrksatukontraktor.konNegeri, kosprojek=intcomma(dataobject.mrksatukosprojek), tarikhmula=dataobject.mrksatutarikhmula, tarikhjsiap=dataobject.mrksatutarikhjangkasiap, pegawai=dataobject.projekbind.nosebuthargaid.pegawaiselia. first_name, jawatan=dataobject.projekbind.nosebuthargaid.pegawaiselia. userprofile.jawatan, tarikhlapo=dataobject.mrksatutarikhdaftar, )) pdfout.write( open('static_in_env/assets/pdf/outputpdf/MRK-01-' + idstr + '.pdf', 'wb')) try: return FileResponse(open( 'static_in_env/assets/pdf/outputpdf/MRK-01-' + idstr + '.pdf', 'rb'), content_type='application/pdf') except FileNotFoundError: raise Http404() return render(request, 'pages/printtest.html')
def pdfskhas02(request, projekid): dataobject = document.SuratKhas.objects.filter(projekbind=projekid).first() lskfetch = document.Laporansiapkerja.objects.filter( projekbind=projekid).first() psk = document.PSK.objects.filter(projekbind=projekid).first() idstr = str(dataobject.projekbind.id) print(idstr) pdfjinja = PdfJinja('static_in_env/assets/pdf/skhas02.pdf') pdfout = pdfjinja( dict( rujukantuan=dataobject.khasrujukantuan, rujukan=dataobject.projekbind, namarujukan=dataobject.khasnamarujukan, alamatrujukan=dataobject.khasalamatrujukan, gred=dataobject.khasmrksatulink.mrksatugred, kategori=dataobject.khasmrksatulink.mrksatukategori, khusus=dataobject.khasmrksatulink.mrksatupengkhususan, kontraktor=dataobject.khasmrksatulink.mrksatukontraktor.konNama, projek=dataobject.projekbind.tajukkerja, jurutera=dataobject.khaspegawai, jawatan=dataobject.khasjawatan, )) pdfout.write( open('static_in_env/assets/pdf/outputpdf/skhas02-' + idstr + '.pdf', 'wb')) try: return FileResponse(open( 'static_in_env/assets/pdf/outputpdf/skhas02-' + idstr + '.pdf', 'rb'), content_type='application/pdf') except FileNotFoundError: raise Http404() return render(request, 'pages/printtest.html')
def res_negatif(self, fullpath, type_dt_dict, noConsultationDuTeleservice, multi_poly_proj, union_line, distance, epsg, min_x, min_y, max_x, max_y): try: name_reseau = self.name_reseau path_output = os.path.join(os.path.abspath(os.path.dirname(__file__)), "output" + os.sep) path_model_cerfa = os.path.join(os.path.abspath(os.path.dirname(__file__)), "cerfa_modele" + os.sep + "cerfa_14435-03.pdf") self.vider_dossier() #calcul de l'echelle obtenu a partir de l'enveloppe du polygone et son buffer scale = self.mapnik_config(min_x, min_y, max_x, max_y) distance = int(distance) pdfjinja = PdfJinja(path_model_cerfa) pdfout = pdfjinja(dict(NoGU=noConsultationDuTeleservice, Echelle1=scale[2], DistanceReseau=distance)) pdfout.write(open(path_output + 'cerfa_' + noConsultationDuTeleservice + '.pdf', 'wb')) #vue carto avec le buffer creer sur emplacement travaux et le calcul de l'enveloppe self.mapnik_output(min_x, min_y, max_x, max_y, epsg, multi_poly_proj, union_line, noConsultationDuTeleservice) print(name_reseau, type_dt_dict, noConsultationDuTeleservice, multi_poly_proj, union_line, distance) os.remove(fullpath) except: print('erreur : objet output, function res -')
def pdfpwjp02(request, projekid): dataobject = document.SuratPelepasanBon.objects.filter( projekbind=projekid).first() lskfetch = document.Laporansiapkerja.objects.filter( projekbind=projekid).first() psk = document.PSK.objects.filter(projekbind=projekid).first() idstr = str(dataobject.projekbind.id) print(idstr) pdfjinja = PdfJinja('static_in_env/assets/pdf/spwjp02.pdf') pdfout = pdfjinja( dict( rujukan=dataobject.projekbind, kepada=dataobject.bonkepada, alamatkepada=dataobject.bonalamatsatu, kontraktor=dataobject.bonmrksatulink.mrksatukontraktor.konNama, kepada2=dataobject.bonmelalui, alamatkepada2=dataobject.bonalamatdua, jurutera=dataobject.bonpegawai, jawatan=dataobject.bonjawatan, kosbon=intcomma(dataobject.bonwangjaminan), )) pdfout.write( open('static_in_env/assets/pdf/outputpdf/spwjp02-' + idstr + '.pdf', 'wb')) try: return FileResponse(open( 'static_in_env/assets/pdf/outputpdf/spwjp02-' + idstr + '.pdf', 'rb'), content_type='application/pdf') except FileNotFoundError: raise Http404() return render(request, 'pages/printtest.html')
def pdfsmrkdua(request, projekid): dataobject = document.SuratMRK.objects.filter(projekbind=projekid).first() lskfetch = document.Laporansiapkerja.objects.filter( projekbind=projekid).first() psk = document.PSK.objects.filter(projekbind=projekid).first() idstr = str(dataobject.projekbind.id) print(idstr) pdfjinja = PdfJinja('static_in_env/assets/pdf/spkk2.pdf') pdfout = pdfjinja( dict( namarujukan=dataobject.smrknamarujukan, alamatrujukan=dataobject.smkralamatrujukan, jenisborang=dataobject.smrkjenisborang, kontraktor=dataobject.smrkmrksatulink.mrksatukontraktor.konNama, nopkk=dataobject.smrkmrksatulink.mrksatukontraktor. sijilPPKNoPendaftaran, norujukan=dataobject.smrkrujukantuan, pegawai=dataobject.smrkpegawai, jawatan=dataobject.smrkjawatan, tarikh=dataobject.smrktarikh, )) pdfout.write( open('static_in_env/assets/pdf/outputpdf/spkk2-' + idstr + '.pdf', 'wb')) try: return FileResponse(open( 'static_in_env/assets/pdf/outputpdf/spkk2-' + idstr + '.pdf', 'rb'), content_type='application/pdf') except FileNotFoundError: raise Http404() return render(request, 'pages/printtest.html')
def pdfppwjp(request, projekid): dataobject = document.Perakuanpwjp.objects.filter( projekbind=projekid).first() lskfetch = document.Laporansiapkerja.objects.filter( projekbind=projekid).first() psk = document.PSK.objects.filter(projekbind=projekid).first() idstr = str(dataobject.projekbind.id) print(idstr) pdfjinja = PdfJinja('static_in_env/assets/pdf/ppwjp.pdf') pdfout = pdfjinja( dict( namarujukan=dataobject.namarujukan, alamtrujukan=dataobject.alamatrujukan, kontraktor=dataobject.wjpmrksatulink.mrksatukontraktor.konNama, projek=dataobject.projekbind.tajukkerja, rm=dataobject.koswjp, jurutera=dataobject.projekbind.nosebuthargaid.pegawaiselia. first_name, jawatan=dataobject.projekbind.nosebuthargaid.pegawaiselia. userprofile.jawatan, )) pdfout.write( open('static_in_env/assets/pdf/outputpdf/ppwjp-' + idstr + '.pdf', 'wb')) try: return FileResponse(open( 'static_in_env/assets/pdf/outputpdf/ppwjp-' + idstr + '.pdf', 'rb'), content_type='application/pdf') except FileNotFoundError: raise Http404() return render(request, 'pages/printtest.html')
def pdfjb(request, projekid): dataobject = document.SuratPJaminanbank.objects.filter( projekbind=projekid).first() lskfetch = document.Laporansiapkerja.objects.filter( projekbind=projekid).first() psk = document.PSK.objects.filter(projekbind=projekid).first() idstr = str(dataobject.projekbind.id) print(idstr) pdfjinja = PdfJinja('static_in_env/assets/pdf/jb.pdf') pdfout = pdfjinja( dict( namabank=dataobject.namabank, alamatbank=dataobject.alamatbank, akaunbank=dataobject.rujukanbank, kontraktor=dataobject.jbankmrksatulink.mrksatukontraktor.konNama, alamat=dataobject.alamatpemborongsurat, tarikha=psk.psktarikhmulatanggug, tarikhb=psk.psktarikhtamattanggung, jurutera=dataobject.projekbind.nosebuthargaid.pegawaiselia. first_name, jawatan=dataobject.projekbind.nosebuthargaid.pegawaiselia. userprofile.jawatan, )) pdfout.write( open('static_in_env/assets/pdf/outputpdf/jb-' + idstr + '.pdf', 'wb')) try: return FileResponse(open( 'static_in_env/assets/pdf/outputpdf/jb-' + idstr + '.pdf', 'rb'), content_type='application/pdf') except FileNotFoundError: raise Http404() return render(request, 'pages/printtest.html')
import os from pdfjinja import PdfJinja dirname = os.path.dirname(__file__) template_pdf_file = os.path.join(dirname, 'input.pdf') template_pdf = PdfJinja(template_pdf_file) rendered_pdf = template_pdf({ "Name": "Vijay", "Age": 26, "Gender": "Male", }) output_file = os.path.join(dirname, 'output.pdf') rendered_pdf.write(open(output_file, 'wb'))
def generate_ops(): print("hit") print(dir(request)) # print(request.data) form = json.loads(request.data) data = form["OPSData"] # with open("./test.txt","w") as f: # for key in data: # f.write(str(key) + "\n") output_objects = [] template = PdfJinja("./fillable_ops.pdf") for key in data: #set DSUID to " pdf_data = generate_pdf_data(data[key]) if key == "Summary": pdf_data["DSUID"] = "" deal_source = pdf_data.get("DealSource") seller = pdf_data.get("Seller") status = pdf_data.get("Status") date = pdf_data.get("Date") if deal_source == None: pdf_data["DealSource"] = "No Data Provided" if seller == None: pdf_data["Seller"] = "No Data Provided" if date == None: pdf_data["Date"] = "N/A" if status == None: pdf_data["Status"] = "No Data Provided" #if DealSource not in pdf_data, then add it with "No Data Provided" as the value output_objects.append(pdf_data) rendered = template(pdf_data) pdf_name = "output" + key + ".pdf" output_file = "./output/" + pdf_name # append output_file to a list rendered.write(open(output_file, "wb")) # use pypdftk.concat() on the resulting list output_objects = [] for filename in os.listdir("./output"): path_name = "./output/" + filename output_objects.append(path_name) output_objects.sort(reverse=True)\ output = PdfFileMerger() for pdf in output_objects: pdf_file = PdfFileReader(pdf) output.append(pdf_file) # pdf_file1 = PdfFileReader("./output/outputSummary.pdf") # pdf_file2 = PdfFileReader("./output/output151097_29303132-1.pdf") # pdf_file3 = PdfFileReader("./output/output151097_17181920-1.pdf") # output.append(pdf_file1) # output.append(pdf_file2) # output.append(pdf_file3) output.write("outputMerged.pdf") # generated_pdf = pypdftk.fill_form('./output/', pdf_data) # output_file = pypdftk.concat(['./outputSummary/', generated_pdf]) # given_value = OPSData.value # form = request.form # data = json.loads(form["OPSData"]) # pdf_data = generate_pdf_data(data['151097_2635-1']) # # print(template) with open("./output.txt", "w") as f: json.dump(output_objects, f, indent=2) return flask.jsonify({"message": "hi"})
from pdfjinja import PdfJinja mypdf = PdfJinja('sydjango3.pdf') pdfout = mypdf({ 'name': 'yoongie', 'sydney': True, 'address': 'test address', 'language': 'javascript' }) with open('filled.pdf', 'wb') as f: pdfout.write(f)
def pdfmrktiga(request, projekid): dataobject = document.MRKTiga.objects.filter(projekbind=projekid).first() lskfetch = document.Laporansiapkerja.objects.filter( projekbind=projekid).first() mrkduaf = document.MRKDua.objects.filter(projekbind=projekid).first() idstr = str(dataobject.projekbind.id) print(idstr) pdfjinja = PdfJinja('static_in_env/assets/pdf/MRK03.pdf') pdfout = pdfjinja( dict( pkk=dataobject.marktigamrksatu.mrksatukontraktor. sijilPPKNoPendaftaran, kontraktor=dataobject.marktigamrksatu.mrksatukontraktor.konNama, sebutharga=dataobject.projekbind.nosebuthargaid, inden=dataobject.marktigamrksatu.mrksatunoinden, tajuk=dataobject.projekbind.tajukkerja, kosprojek=intcomma(dataobject.marktigamrksatu.mrksatukosprojek), kossebenar=intcomma(lskfetch.lskhargasebenar), tarikhmula=dataobject.marktigamrksatu.mrksatutarikhmula, tarikhakhir=lskfetch.lskkerjasiap, lanjuthingga=lskfetch.lsklanjutmasa, tarikhsiapsebenar=lskfetch.lskkerjasiap, lad=mrkduaf.mrkduaLAD, laddari=mrkduaf.mrkduaLADdari, ladhingga=mrkduaf.mrkduaLADSehingga, c1a=dataobject.mrktigabina, c1b=dataobject.mrktigabina, c1c=dataobject.mrktigabina, c1d=dataobject.mrktigabina, c2a=dataobject.mrktigatadbir, c2b=dataobject.mrktigatadbir, c2c=dataobject.mrktigatadbir, c2d=dataobject.mrktigatadbir, c3a=dataobject.mrktigakemajuan, c3b=dataobject.mrktigakemajuan, c3c=dataobject.mrktigakemajuan, c3d=dataobject.mrktigakemajuan, c4a=dataobject.mkrtigamutukerangka, c4b=dataobject.mkrtigamutukerangka, c4c=dataobject.mkrtigamutukerangka, c4d=dataobject.mkrtigamutukerangka, c5a=dataobject.mrktigamutukerja, c5b=dataobject.mrktigamutukerja, c5c=dataobject.mrktigamutukerja, c5d=dataobject.mrktigamutukerja, c6a=dataobject.mrktigamutukemasan, c6b=dataobject.mrktigamutukemasan, c6c=dataobject.mrktigamutukemasan, c6d=dataobject.mrktigamutukemasan, c7a=dataobject.mrktigamutukerjaluar, c7b=dataobject.mrktigamutukerjaluar, c7c=dataobject.mrktigamutukerjaluar, c7d=dataobject.mrktigamutukerjaluar, c8a=dataobject.mrktigapegawasan, c8b=dataobject.mrktigapegawasan, c8c=dataobject.mrktigapegawasan, c8d=dataobject.mrktigapegawasan, catat1=dataobject.mrkcatat1, catat2=dataobject.mrkcatat2, catat3=dataobject.mrkcatat3, catat4=dataobject.mrkcatat4, catat5=dataobject.mrkcatat5, catat6=dataobject.mrkcatat6, catat7=dataobject.mrkcatat7, catat8=dataobject.mrkcatat8, ulasan=dataobject.mrktigasokongan, )) pdfout.write( open('static_in_env/assets/pdf/outputpdf/MRK03-' + idstr + '.pdf', 'wb')) try: return FileResponse(open( 'static_in_env/assets/pdf/outputpdf/MRK03-' + idstr + '.pdf', 'rb'), content_type='application/pdf') except FileNotFoundError: raise Http404() return render(request, 'pages/printtest.html')