Add Image to PDF - PHP Sample Code

Sample code to use Apryse SDK for programmatically inserting various raster image formats (e.g. TIFF, JPEG, JPEG2000, JBIG2, GIF, PNG, BMP, etc.) into a PDF document. Sample code provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby and VB.

Learn more about our Server SDK and PDF Editing & Manipulation Library.

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//-----------------------------------------------------------------------------------
11// This sample illustrates how to embed various raster image formats
12// (e.g. TIFF, JPEG, JPEG2000, JBIG2, GIF, PNG, BMP, etc.) in a PDF document.
13//
14// Note: On Windows platform this sample utilizes GDI+ and requires GDIPLUS.DLL to
15// be present in the system path.
16//-----------------------------------------------------------------------------------
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 // Relative path to the folder containing the test files.
22 $input_path = getcwd()."/../../TestFiles/";
23 $output_path = $input_path."Output/";
24
25 $doc = new PDFDoc();
26 $builder = new ElementBuilder(); // Used to build new Element objects
27 $writer = new ElementWriter(); // Used to write Elements to the page
28
29 $page = $doc->PageCreate(); // Start a new page
30 $writer->Begin($page); // Begin writing to this page
31
32 // ----------------------------------------------------------
33 // Add JPEG image to the output file
34 $img = Image::Create($doc->GetSDFDoc(), $input_path."peppers.jpg");
35 $element = $builder->CreateImage($img, 50.0, 500.0, (double)($img->GetImageWidth())/2, (double)($img->GetImageHeight())/2);
36 $writer->WritePlacedElement($element);
37
38 // ----------------------------------------------------------
39 // Add a PNG image to the output file
40 $img = Image::Create($doc->GetSDFDoc(), $input_path."butterfly.png");
41 $element = $builder->CreateImage($img, new Matrix2D(100.0, 0.0, 0.0, 100.0, 300.0, 500.0));
42 $writer->WritePlacedElement($element);
43
44 // ----------------------------------------------------------
45 // Add a GIF image to the output file
46 $img = Image::Create($doc->GetSDFDoc(), $input_path."pdfnet.gif");
47 $element = $builder->CreateImage($img, new Matrix2D((double)($img->GetImageWidth()), 0.0, 0.0, (double)($img->GetImageHeight()), 50.0, 350.0));
48 $writer->WritePlacedElement($element);
49
50 // ----------------------------------------------------------
51 // Add a TIFF image to the output file
52
53 $img = Image::Create($doc->GetSDFDoc(), $input_path."grayscale.tif");
54 $element = $builder->CreateImage($img, new Matrix2D((double)($img->GetImageWidth()), 0.0, 0.0, (double)($img->GetImageHeight()), 10.0, 50.0));
55 $writer->WritePlacedElement($element);
56
57 $writer->End(); // Save the page
58 $doc->PagePushBack($page); // Add the page to the document page sequence
59
60 // ----------------------------------------------------------
61 // Embed a monochrome TIFF. Compress the image using lossy JBIG2 filter.
62
63 $page = $doc->PageCreate(new Rect(0.0, 0.0, 612.0, 794.0));
64 $writer->Begin($page); // begin writing to this page
65
66 // Note: encoder hints can be used to select between different compression methods.
67 // For example to instruct PDFNet to compress a monochrome image using JBIG2 compression.
68 $hint_set = new ObjSet();
69 $enc = $hint_set->CreateArray(); // Initilaize encoder 'hint' parameter
70 $enc->PushBackName("JBIG2");
71 $enc->PushBackName("Lossy");
72
73 $img = Image::Create($doc->GetSDFDoc(), $input_path."multipage.tif");
74 $element = $builder->CreateImage($img, new Matrix2D(612.0, 0.0, 0.0, 794.0, 0.0, 0.0));
75 $writer->WritePlacedElement($element);
76
77 $writer->End(); // Save the page
78 $doc->PagePushBack($page); // Add the page to the document page sequence
79
80 // ----------------------------------------------------------
81 // Add a JPEG2000 (JP2) image to the output file
82
83 // Create a new page
84 $page = $doc->PageCreate();
85 $writer->Begin($page); // Begin writing to the page
86
87 // Embed the image
88 $img = Image::Create($doc->GetSDFDoc(), $input_path."palm.jp2");
89
90 // Position the image on the page
91 $element = $builder->CreateImage($img, new Matrix2D((double)($img->GetImageWidth()), 0.0, 0.0, (double)($img->GetImageHeight()), 96.0, 80.0));
92 $writer->WritePlacedElement($element);
93
94 // Write 'JPEG2000 Sample' text string under the image
95 $writer->WriteElement($builder->CreateTextBegin(Font::Create($doc->GetSDFDoc(), Font::e_times_roman), 32.0));
96 $element = $builder->CreateTextRun("JPEG2000 Sample");
97 $element->SetTextMatrix(1.0, 0.0, 0.0, 1.0, 190.0, 30.0);
98 $writer->WriteElement($element);
99 $writer->WriteElement($builder->CreateTextEnd());
100
101 $writer->End(); // Finish writing to the page
102 $doc->PagePushBack($page);
103
104 $doc->Save(($output_path."addimage.pdf"), SDFDoc::e_linearized);
105 $doc->Close();
106 PDFNet::Terminate();
107 echo nl2br("Done. Result saved in addimage.pdf...\n");
108?>

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales