File: C:/xampp/htdocs/dropfile/php/uploads/pdfResize.py
from sys import exc_info
from traceback import print_tb
from PDFlib.PDFlib import *
import time
ts = time.time()
import datetime
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
import os
import os
dirlist = os.listdir("C:/adfs/pTrans3")
from pprint import pprint
pprint(dirlist)
searchpath = "C:/adfs/pTrans3"
imagefile = "blank.jpg"
outfile = ""
title = "PDF Impose"
pdffiles = dirlist
p = PDFlib()
try:
p.set_option("errorpolicy=return")
p.set_option("license=w900602-010300-741796-5KVCF2-Q4PLF2")
# Set the search path for fonts and PDF files
p.set_option("SearchPath={{" + searchpath +"}}")
strp = pdffiles[0]
print ("fName:"+strp[:-12])
print ("stp:"+strp[-8:-5])
print ("stp2:"+strp[-12:-9])
import re
text = pdffiles[0]
print(re.split('_',text))
pp=re.split('_',text)
pname = pp[1]
s = pdffiles[0]
ix = s.split('_', 2)
name = ix[0]
name2 = ix[1]
name3 = ix[2]
print ("domain:"+pname )
print ("domain2:"+name2 )
size=pname.split('x', 1)
width=size[0]
height=size[1]
print ("width:"+width )
print ("height:"+height )
#if p.begin_document("C:/adfs/Xout/"+rn+r2+".pdf", "") == -1:
# raise PDFlibException("Error: " + p.get_errmsg())
#if p.begin_document("C:/adfs/t2/"+rn+r2+".pdf", "") == -1:
# raise PDFlibException("Error: " + p.get_errmsg())
scale = 1.0
pageopen = 0
indocP = p.open_pdi_document(pdffiles[0], "")
pwidth = p.pcos_get_number(indocP, "pages[0]/width");
pheight = p.pcos_get_number(indocP, "pages[0]/height");
#pwidthAr = p.pcos_get_number(indocP, "pages[0]/ArtBox[2]");
#pheightAr = p.pcos_get_number(indocP, "pages[0]/ArtBox[3]");
pwidthBr = p.pcos_get_number(indocP, "pages[0]/BleedBox[2]");
pheightBr = p.pcos_get_number(indocP, "pages[0]/BleedBox[3]");
pwidthTr = p.pcos_get_number(indocP, "pages[0]/TrimBox[2]");
pheightTr = p.pcos_get_number(indocP, "pages[0]/TrimBox[3]");
pwidthTr0 = p.pcos_get_number(indocP, "pages[0]/TrimBox[0]");
pheightTr0 = p.pcos_get_number(indocP, "pages[0]/TrimBox[1]");
endpage = p.pcos_get_number(indocP, "length:pages")
trimwidth=p.pcos_get_number(indocP, "pages[0]/TrimBox[2]")-p.pcos_get_number(indocP, "pages[0]/TrimBox[0]");
trimheight=p.pcos_get_number(indocP, "pages[0]/TrimBox[3]")-p.pcos_get_number(indocP, "pages[0]/TrimBox[1]");
xcenetr=p.pcos_get_number(indocP, "pages[0]/TrimBox[0]")+(p.pcos_get_number(indocP, "pages[0]/TrimBox[2]")-p.pcos_get_number(indocP, "pages[0]/TrimBox[0]"))/2;
m=2.8346
if abs((float(width)*m)-float(trimwidth))>6 :
ratio=(float(width)*m)/float(trimwidth)
else :
ratio=1
#ratio=(float(width)*m)/float(trimwidth)
print("ration:"+str(ratio)+" \n")
print("Warning:"+pdffiles[0]+" \n")
print("width:"+str(pwidth/m)+" \n")
print("heigtht:"+str(pheight/m)+" \n")
#print("widthAr:"+str(pwidthAr/m)+" \n")
#print("heigthtAr:"+str(pheightAr/m)+" \n")
print("widthBr:"+str(pwidthBr/m)+" \n")
print("heigtBtTr:"+str(pheightBr/m)+" \n")
print("widthTr:"+str(pwidthTr/m)+" \n")
print("heigthtTr:"+str(pheightTr/m)+" \n")
print("center:"+str(xcenetr/m)+" \n")
print("Trx:"+str(pwidthTr0/m)+" \n")
print("Try:"+str(pheightTr0/m)+" \n")
print("endpage:"+str(endpage)+" \n")
print("endpage2:"+str(pwidthTr-(pwidth-pwidthTr))+" \n")
xgap=pwidth-pwidthTr
ygap=pheight-pheightTr
print("xgap:"+str(xgap/m)+" \n")
print("ygap:"+str(ygap/m)+" \n")
######################################start doc
# dummy page size, will be adjusted by p.fit_image()############page begin
for i in range(0, int(endpage), 1):
if p.begin_document("C:/adfs/Xout/"+name3, "") == -1:
raise PDFlibException("Error: trimwidth" + p.get_errmsg())
p.set_info("Creator", "placePdf.py")
p.set_info("Author", "Fred Kuo")
p.set_info("Title","PDFlib block processing sample (C)")
#p.begin_page_ext(trimwidth/2+xgap*2, trimheight+ygap*2, "trimbox={"+str(xgap)+" "+str(ygap)+ " "+str(trimwidth/2+xgap)+ " "+ str(trimheight+ygap)+" }")
#p.begin_page_ext(float(width)*m, float(height)*m, "trimbox={"+str(3*m)+" "+str(3*m)+ " "+str(float(width)*m-3*m)+ " "+ str(float(height)*m-3*m)+" }")
p.begin_page_ext(float(width)*m, float(height)*m, "")
pwidth=float(width)*m
pheight=float(height)*m
xcenetr=pwidth/2
# Set the line widthxcenetr
p.setlinewidth(0.5)
blockcontainer = p.open_pdi_document(pdffiles[0], "")
if blockcontainer == -1:
raise PDFlibException("Error:pwidth " + p.get_errmsg())
pages = p.open_pdi_page(blockcontainer, i+1, "")
#image= p.open_pdi_page(pages, 1,"boxsize={87 123 508 718}")
optlist="boxsize={"+str(pwidth)+" "+str(pheight)+"} position=center fitmethod=meet"
p.fit_pdi_page(pages, 0, 0, optlist); ############left end
######################################################################################################
p.end_page_ext("")
p.end_document("")
###########################################################################################################
except PDFlibException:
print("PDFlib exception occurred:\n[%d] %s: %s" %
((p.get_errnum()), p.get_apiname(), p.get_errmsg()))
print_tb(exc_info()[2])
except Exception:
print("Exception occurred: %s" % (exc_info()[0]))
print_tb(exc_info()[2])
finally:
p.delete()
source = 'C:/adfs/pTrans4'
destination = 'Y:/MacWork3/PDF-COV加摺線/ORG'
import shutil
for file in os.listdir(source):
#print file # testing
shutil.move(source+"/"+pdffiles[0], destination+"/"+pdffiles[0])