Are you searching for a option to embed a PDF file in your web site with out counting on exterior providers or plugins? If that’s the case, then you definitely’re in luck! On this tutorial, we’ll present you methods to use PHP and cURL to obtain the content material of a PDF file from a URL and show it in an iframe.
First, let’s begin by making a operate that makes use of cURL to obtain the PDF file:
operate displayPdfFromUrl($url) {
// Initialize cURL session
$ch = curl_init();
// Set cURL choices
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute cURL request
$information = curl_exec($ch);
// Shut cURL session
curl_close($ch);
// Return PDF file content material
return $information;
}
This operate takes a URL as an argument and makes use of cURL to obtain the content material of the PDF file at that URL. The content material is then returned as a string.
Subsequent, we will use the base64_encode operate to encode the PDF information as a base64 string, which we will then use because the src attribute for an iframe aspect:
That is all there may be to it! With only a few strains of code, you may simply show PDF information in your web site with out counting on exterior providers or plugins.