Guest User

Untitled

a guest
Nov 13th, 2018
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. # This is free and unencumbered software released into the public domain.
  2.  
  3. # Anyone is free to copy, modify, publish, use, compile, sell, or
  4. # distribute this software, either in source code form or as a compiled
  5. # binary, for any purpose, commercial or non-commercial, and by any
  6. # means.
  7.  
  8. # In jurisdictions that recognize copyright laws, the author or authors
  9. # of this software dedicate any and all copyright interest in the
  10. # software to the public domain. We make this dedication for the benefit
  11. # of the public at large and to the detriment of our heirs and
  12. # successors. We intend this dedication to be an overt act of
  13. # relinquishment in perpetuity of all present and future rights to this
  14. # software under copyright law.
  15.  
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  19. # IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. # OTHER DEALINGS IN THE SOFTWARE.
  23.  
  24. import subprocess, sys, os, tempfile
  25. import atexit, shutil
  26.  
  27. prefix = "BookmarkPageNumber: "
  28.  
  29. split_range = int(sys.argv[2])
  30. info = subprocess.check_output(["pdftk", sys.argv[1], "dump_data"])
  31. appendix = []
  32. main_paper = []
  33. curr_bookmark = None
  34. for i in info.split("\n"):
  35. if i == "BookmarkBegin":
  36. if curr_bookmark is not None:
  37. (page, bk) = curr_bookmark
  38. if page > split_range:
  39. appendix += bk
  40. else:
  41. main_paper += bk
  42. curr_bookmark = (None, [])
  43. if i.startswith(prefix):
  44. num = int(i[len(prefix):])
  45. curr_bookmark = (num, curr_bookmark[1])
  46. if num > split_range:
  47. i = prefix + str(num - 29)
  48. if curr_bookmark is not None:
  49. curr_bookmark[1].append(i)
  50.  
  51. tmp = tempfile.mkdtemp()
  52. atexit.register(lambda: shutil.rmtree(tmp))
  53.  
  54. def update_info(tmp_file, info, output_name):
  55. new_info = subprocess.check_output(["pdftk", tmp + "/" + tmp_file + ".pdf", "dump_data"])
  56. with open(os.path.join(tmp, tmp_file + ".info"), "w") as f:
  57. print >> f, new_info
  58. print >> f, "\n".join(info)
  59. subprocess.check_call(["pdftk", tmp + "/" + tmp_file + ".pdf", "update_info", os.path.join(tmp, tmp_file + ".info"), "output", output_name])
  60.  
  61. subprocess.check_call(["pdftk", sys.argv[1], "cat", "1-%d" % split_range, "output", tmp + "/tmp_main.pdf"])
  62. subprocess.check_call(["pdftk", sys.argv[1], "cat", "%d-end" % (split_range + 1), "output", tmp + "/tmp_appendix.pdf"])
  63. update_info("tmp_main", main_paper, sys.argv[1])
  64. update_info("tmp_appendix", appendix, sys.argv[3])
Add Comment
Please, Sign In to add comment