skip420

upload

Sep 6th, 2021 (edited)
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 3.07 KB | None | 0 0
  1. #This can be used for server or localhost
  2. #Working file browse and upload
  3. #upload.html
  4.  
  5.  
  6.  
  7.  
  8. <html>
  9. <body>
  10. <form enctype='multipart/form-data' action='api.py' method='post'>
  11. <p>File: <input type='file' name='filename' /></p>
  12. <p><input type='submit' value='Upload' name='action'/></p>
  13. </form>
  14. </body>
  15. </html>
  16.  
  17. ============================================================================================================================
  18. #API
  19. #api.py
  20. #python3 api.py
  21.  
  22.  
  23. import cgi
  24. import os
  25. import sys
  26. import popplerqt5
  27. import PyQt5
  28. import PyPDF2
  29.  
  30.  
  31.  
  32.  
  33. def ExtractHighlights(InFile):
  34.     Highlights = []
  35.     doc = popplerqt5.Poppler.Document.load(InFile)
  36.     total_annotations = 0
  37.     for x in range(doc.numPages()):
  38.         page = doc.page(x)
  39.         annotations = page.annotations()
  40.         (pwidth, pheight) = (page.pageSize().width(), page.pageSize().height())
  41.         if len(annotations) > 0:
  42.             for annotation in annotations:
  43.                 if  isinstance(annotation, popplerqt5.Poppler.Annotation):
  44.                     total_annotations += 1
  45.                     if(isinstance(annotation, popplerqt5.Poppler.HighlightAnnotation)):
  46.                         quads = annotation.highlightQuads()
  47.                         txt = ""
  48.                         for quad in quads:
  49.                             rect = (quad.points[0].x() * pwidth,
  50.                                     quad.points[0].y() * pheight,
  51.                                     quad.points[2].x() * pwidth,
  52.                                     quad.points[2].y() * pheight)
  53.                             bdy = PyQt5.QtCore.QRectF()
  54.                             bdy.setCoords(*rect)
  55.                             txt = txt + str(page.text(bdy)) + ' '
  56.  
  57.                         # Clean up the string a bit
  58.                         txt = txt.lstrip()
  59.                         txt = txt.rstrip()
  60.                        
  61.                         Highlights.append(txt + ',' + str(x))
  62.  
  63.     if (total_annotations < 1):
  64.        return '0'
  65.    
  66.    return Highlights
  67.  
  68. def BuildReport(Data):
  69.    # Build Report of highlighted words, each word only once,
  70.    # if the word occurs on more than one page - record the word only once and append it's page numbers
  71.    FinalList = ''
  72.    for x in range(len(Data)):
  73.        Pages = ''
  74.        for y in range(len(Data)):
  75.            if(Data[x].split(',')[0] == Data[y].split(',')[0]):
  76.              Pages = Pages + Data[y].split(',')[1] + ','
  77.        Pages = Pages.rstrip(',')
  78.        FinalList = FinalList + Data[x].split(',')[0] + ',' +  Pages + '-'
  79.    FinalList = FinalList.rstrip('-')
  80.    return FinalList
  81. form      = cgi.FieldStorage()
  82. TempPath  = '/var/www/html/PDFtool/tmp/'
  83. fileitem = form['filename']
  84. # Test if the file is loaded for the upload
  85. if (fileitem.filename):
  86.    fn = os.path.basename(fileitem.filename)
  87.    open(TempPath + fn, 'wb').write(fileitem.file.read())
  88.    print ('Content-type:text/html\r\n')
  89.    print (BuildReport(ExtractHighlights(TempPath + fn)))
  90. else:
  91.    print ('Content-type:text/html\r\n')
  92.    print ('Error')
Add Comment
Please, Sign In to add comment