1

I have a blank pdf file.i get some content in bytes and wrote in this pdf file. Again i got some bytes and i want to append it in existing pdf file. I tried to open pdf file in 'ab' and 'a+b' mode but content is not appending its overwriting . Please help me to achieve this.

Here is the code -

get_view_pdf_url = "http://localhost:8000/api/3.0/sites/a18ab9c9-c523-4848-9199-8c50e100ac36/views/e948dd8c-31b1-480a-8123-fecf47b1c682/pdf"
reqPDF = requests.get(get_view_pdf_url, data=b'', headers={'x-tableau-auth': token})
reqPDF.raise_for_status()
print((reqPDF.content))
filename = "C:/Users/pinki.sharma/Desktop/Work/tempmon/pinkiTest.pdf"
with open(filename, 'wb') as f:
    f.write(reqPDF.content)
    f.close()

get_view_pdf1_url = "http://localhost:8000/api/3.0/sites/a18ab9c9-c523-4848-9199-8c50e100ac36/views/910d1215-59cb-4d63-95fd-7a2b224f18bc/pdf"
reqPDF1 = requests.get(get_view_pdf1_url, data=b'', headers={'x-tableau-auth': token})
reqPDF1.raise_for_status()
print((reqPDF1.content))

with open(filename, 'a+b') as f1:
    f1.write(reqPDF1.content)
    f1.close()
mkl
  • 90,588
  • 15
  • 125
  • 265
Pinki Sharma
  • 121
  • 2
  • 16
  • pdf documents are structured documents, nothing you can simply "append" to - are you sure that there is even a slight possibility that what you do *might* work? what are the file sizes after your 1st append, how many bytes in in the 2nd payload and how big is the file afterwards? – Patrick Artner May 16 '18 at 06:32
  • I wrote byte content in different files and i found first file size is - 1.22 MB. and second file size is-129 KB . – Pinki Sharma May 16 '18 at 09:01
  • I dint mention any combined size in my code but i merged these two pdf using merger. Size of merged pdf = 1.32 MB. – Pinki Sharma May 17 '18 at 06:20
  • If you want to just append all pages of your 2nd pdf to your 1st one, just download the second into a seperate file and merge both afterwards: [here](https://stackoverflow.com/a/38128675/7505395) – Patrick Artner May 17 '18 at 06:45
  • Thanks for helping . Yes, i achieved this functionality using same way but i was wondering that i am not able to append in same file.Due to this , now I have to create multiple pdfs and merge them . Have to delete old pdfs and keep the last one(merged). – Pinki Sharma May 17 '18 at 07:39

1 Answers1

0

PDFs are complex structured documents, if you just glue 2 complete pdfs byte-wise one after the other, you get a bigger file but only will see the last document.

It is as if you have 10 pages and you glue one on top of the next - your stack gets bigger and heavier but you can only ever access the top one.

If you want to add pages of one pdf to another, you have to insert them into the correct parts of the first pdf - one way to do this is described in this answer by @ImportanceOfBeingErnest:

You may want to use pyPdf for this.

    # Merge two PDFs
    from pyPdf import PdfFileReader, PdfFileWriter
     
    output = PdfFileWriter()
    pdfOne = PdfFileReader(file( "some\path\to\a\PDf", "rb"))
    pdfTwo = PdfFileReader(file("some\other\path\to\a\PDf", "rb"))
     
    output.addPage(pdfOne.getPage(0))
    output.addPage(pdfTwo.getPage(0))
     
    outputStream = file(r"output.pdf", "wb")
    output.write(outputStream)
    outputStream.close()

example taken from here

Community
  • 1
  • 1
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • Thanks @patrick Artner for replying. I achieved this using pypdf2 library. In this case i have to create multiple pdfs and merge them. Is it possible to use only one pdf file, open in append mode and write the bytes content. I tried this but not working – Pinki Sharma May 23 '18 at 11:31
  • Pdfs are structured. Whatever pdf-library you use, it will insert the needed data into different parts of the pdf - simply appending does not work. – Patrick Artner May 23 '18 at 14:21