Read & Write PDF in Memory Buffer - PHP 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<?php
2//---------------------------------------------------------------------------------------
3// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
4// Consult LICENSE.txt regarding license information.
5//---------------------------------------------------------------------------------------
6if(file_exists("../../../PDFNetC/Lib/PDFNetPHP.php"))
7include("../../../PDFNetC/Lib/PDFNetPHP.php");
8include("../../LicenseKey/PHP/LicenseKey.php");
9
10// Relative path to the folder containing the test files.
11$input_path = getcwd()."/../../TestFiles/";
12$output_path = $input_path."Output/";
13
14 // The following sample illustrates how to read/write a PDF document from/to
15 // a memory buffer. This is useful for applications that work with dynamic PDF
16 // documents that don't need to be saved/read from a disk.
17
18 PDFNet::Initialize($LicenseKey);
19 PDFNet::GetSystemFontList(); // Wait for fonts to be loaded if they haven't already. This is done because PHP can run into errors when shutting down if font loading is still in progress.
20
21 // Read a PDF document in a memory buffer.
22 $file = new MappedFile($input_path."tiger.pdf");
23 $file_sz = $file->FileSize();
24
25 $file_reader = new FilterReader($file);
26 $mem = $file_reader->Read($file_sz);
27 $test = array();
28 for ($i = 0; $i < strlen($mem); $i++) {
29 $test[] = ord($mem[$i]);
30 }
31 $doc = new PDFDoc($mem, $file_sz);
32 $doc->InitSecurityHandler();
33 $num_pages = $doc->GetPageCount();
34
35 $writer = new ElementWriter();
36 $reader = new ElementReader();
37
38 // Create a duplicate of every page but copy only path objects
39 for($i=1; $i<=$num_pages; ++$i)
40 {
41 $itr = $doc->GetPageIterator(2*$i-1);
42
43 $reader->Begin($itr->Current());
44 $new_page = $doc->PageCreate($itr->Current()->GetMediaBox());
45 $next_page = $itr;
46 $next_page->Next();
47 $doc->PageInsert($next_page, $new_page);
48
49 $writer->Begin($new_page);
50 while (($element = $reader->Next()) !=null) // Read page contents
51 {
52 //if ($element->GetType() == Element::e_path)
53 $writer->WriteElement($element);
54 }
55
56 $writer->End();
57 $reader->End();
58 }
59
60 $doc->Save($output_path."doc_memory_edit.pdf", SDFDoc::e_remove_unused);
61
62 // Save the document to a memory buffer.
63 $buffer = $doc->Save(SDFDoc::e_remove_unused);
64
65 // Write the contents of the buffer to the disk
66 $outfile = fopen($output_path."doc_memory_edit.txt", "w");
67 fwrite($outfile, $buffer);
68 fclose($outfile);;
69
70 // Read some data from the file stored in memory
71 $reader->Begin($doc->GetPage(1));
72 while (($element = $reader->Next()) !=null) {
73 if ($element->GetType() == Element::e_path) echo "Path, ";
74 }
75 $reader->End();
76 PDFNet::Terminate();
77 echo nl2br("\n\nDone. Result saved in doc_memory_edit.pdf and doc_memory_edit.txt ...\n");
78?>

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales