0

I am making a script that batch converts pdf from 1.5 or above to 1.4

After conversion, I need to prepend header tags that were previously there. Whenever I do this through the Python script I wrote, the pdf returns an error

"There was an error while reading this Stream"

But when I do it manually wherein I open the pdf with Notepad++ and paste the headers manually, it works just fine.

What I've done to figure out the problem was to compare the ones that were manually added and the one where the script added the header.

Upon doing so, there seems to be a lot of changes that were made when I just pasted the header like

/CreationDate(D:20200728024434+08'00')
/ModDate(D:20200728024434+08'00')
/ID [<BCBBEC32B429CF4C4EC06D5317B6B1FE><BCBBEC32B429CF4C4EC06D5317B6B1FE>]

to

/CreationDate(D:20200728024353+08'00')
/ModDate(D:20200728024353+08'00')
/ID [<8C36C8F0530094A4C7058CC0845A2FDC><8C36C8F0530094A4C7058CC0845A2FDC>] 

Is there some sort of fix that Notepad++ do whenever I edit the file and is there a way I can do the same automatically through my script so that I won't have to paste the headers manually.

EDIT: I made a mistake and Notepad++ wasn't really doing anything to the pdf. Is there a way that I can paste the headers automatically without it being corrupted?

In my python code, what I do is

    content = f.read()
    f.seek(0, 0)
    f.write(header1 + header2 + content)
    f.close()`

I found this way to add the headers in this article

  • From your description it isn't clear whether this is something Notepad++ does, or your python script does. An editor shouldn't change the information that you copypaste. So I am a bit at loss as to what is actually happening here. Could you describe your process exactly and maybe list the relevant python code part? – Diego Jul 27 '20 at 19:12
  • In my python code, what I do is `with open(outputPath, 'r+', encoding="ansi") as f: content = f.read() f.seek(0, 0) f.write(header1 + header2 + content) f.close()` I found this solution in this [link](https://stackoverflow.com/questions/5914627/prepend-line-to-beginning-of-a-file/5917395) As for the Notepad++ changing parts of the pdf, I was wrong and that there are no changes that Notepad++ does. – Juan Miguel C. Roman Jul 27 '20 at 19:37
  • 1
    What do you mean "converting from PDF 1.5 to 1.4"? I hope you're not just changing the version number in the PDF because that would not be the right way of doing it. Regardless, it looks like you're opening this file as a text file - PDF files are not text files and can't be treated like that. At least open, read and write it as binary. – David van Driessche Jul 27 '20 at 20:09

0 Answers0