Read & Write PDF in Memory Buffer - Python Sample Code

Sample code for using Apryse SDK to read/write a PDF document from/to memory buffer; provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby, Go and VB. This is useful for applications that work with dynamic PDFdocuments that don't need to be saved/read from a disk. Learn more about our Server SDK.

1#---------------------------------------------------------------------------------------
2# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6import site
7site.addsitedir("../../../PDFNetC/Lib")
8import sys
9from PDFNetPython import *
10
11sys.path.append("../../LicenseKey/PYTHON")
12from LicenseKey import *
13
14def main():
15 PDFNet.Initialize(LicenseKey)
16
17 # Relative path to the folder containing the test files.
18 input_path = "../../TestFiles/"
19 output_path = "../../TestFiles/Output/"
20
21 # The following sample illustrates how to read/write a PDF document from/to
22 # a memory buffer. This is useful for applications that work with dynamic PDF
23 # documents that don't need to be saved/read from a disk.
24
25 # Read a PDF document in a memory buffer.
26 file = MappedFile(input_path + "tiger.pdf")
27 file_sz = file.FileSize()
28
29 file_reader = FilterReader(file)
30
31 mem = file_reader.Read(file_sz)
32 doc = PDFDoc(bytearray(mem), file_sz)
33 doc.InitSecurityHandler()
34 num_pages = doc.GetPageCount()
35
36 writer = ElementWriter()
37 reader = ElementReader()
38 element = Element()
39
40 # Create a duplicate of every page but copy only path objects
41
42 i = 1
43 while i <= num_pages:
44 itr = doc.GetPageIterator(2*i - 1)
45
46 reader.Begin(itr.Current())
47 new_page = doc.PageCreate(itr.Current().GetMediaBox())
48 next_page = itr
49 next_page.Next()
50 doc.PageInsert(next_page, new_page)
51
52 writer.Begin(new_page)
53 element = reader.Next()
54 while element != None: # Read page contents
55 #if element.GetType() == Element.e_path:
56 writer.WriteElement(element)
57 element = reader.Next()
58 writer.End()
59 reader.End()
60 i = i + 1
61
62 doc.Save(output_path + "doc_memory_edit.pdf", SDFDoc.e_remove_unused)
63
64 # Save the document to a memory buffer
65 buffer = doc.Save(SDFDoc.e_remove_unused)
66
67 # Write the contents of the buffer to the disk
68 if sys.version_info.major >= 3:
69 f = open(output_path + "doc_memory_edit.txt", "w")
70 else:
71 f = open(output_path + "doc_memory_edit.txt", "wb")
72 try:
73 f.write(str(buffer))
74 finally:
75 f.close()
76
77 # Read some data from the file stored in memory
78 reader.Begin(doc.GetPage(1))
79 element = reader.Next()
80 while element != None:
81 if element.GetType() == Element.e_path:
82 sys.stdout.write("Path, ")
83 element = reader.Next()
84 reader.End()
85
86 PDFNet.Terminate()
87 print("\n\nDone. Result saved in doc_memory_edit.pdf and doc_memory_edit.txt ...")
88
89if __name__ == '__main__':
90 main()

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales