php小編蘋果將為您介紹如何在PHP中將PDF文件轉換為預覽影像。 PDF文件在網頁上展示時,通常需要將其轉換為圖像格式以提供預覽功能。透過PHP程式碼,我們可以利用第三方函式庫實現PDF到影像的轉換,從而方便使用者線上預覽PDF文件。本文將介紹具體的程式碼實作步驟,讓您輕鬆實現PDF文件的預覽功能。
預覽為您的內容增加了一層安全性性,因為圖像上的內容無法複製和貼上。它們還提供其他我們不會詳述的功能。
將 PDF 文件轉換為預覽影像的最簡單方法是利用第三方程式庫。這些都是:
# 此命令列實用程式可在 windows、linux 和 Mac 上使用。請依照下列步驟將 PDF 文件轉換為預覽影像。
<code> <code class="language-bash hljs"><span class="hljs-variable">$gs</span> --vers<strong class="keylink">io</strong>n </code></code>
-dTextAlphaBits=4 -dGraphicsAlphaBits=4
-dFirstPage=1 -dLastPage=1 -r300
-sOutputFile=preview.jpg input.pdf
<code> <code class="hljs language-php"><span class="hljs-number">4</span>. 上面的命令将在您的文档上创建起始页的图像。 我们将调用 `exec()` 函数来使用 PHP 中的命令,如下所示。 ```php <span class="hljs-meta"><?php</span> exec( <span class="hljs-string">"ls -l"</span>, <span class="hljs-variable">$output_str</span>, <span class="hljs-variable">$return_val</span> ); <span class="hljs-keyword">foreach</span> ( <span class="hljs-variable">$output_str</span> <span class="hljs-keyword">as</span> <span class="hljs-variable">$line</span> ) { <span class="hljs-keyword">echo</span> <span class="hljs-variable">$line</span> . <span class="hljs-string">"\n"</span>; } <span class="hljs-meta">?></span> </code></code>
上面的程式碼會將所有目錄和檔案載入到控制台。我們現在可以使用 PHP 程式碼來執行 Ghostscript 命令。
這是我們使用的 PHP 腳本。
<code> <code class="language-php hljs"><span class="hljs-meta"><?php</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">my_pdf</span> (<span class="hljs-params"> <span class="hljs-variable">$file</span> </span>) </span>{ <span class="hljs-variable">$file_info</span> = file_get_contents( <span class="hljs-variable">$file</span> ); <span class="hljs-keyword">if</span> ( preg_match( <span class="hljs-string">"/^%PDF-[0-1]\.[0-9]+/"</span>, <span class="hljs-variable">$file_info</span> ) ) { <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>; } <span class="hljs-keyword">else</span> { <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>; } } <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">our_preview</span> (<span class="hljs-params"> <span class="hljs-variable">$file</span> </span>) </span>{ <span class="hljs-variable">$our_f<strong class="keylink">ORM</strong>at</span> = <span class="hljs-string">"png"</span>; <span class="hljs-variable">$prvw_page</span> = <span class="hljs-string">"1"</span>; <span class="hljs-variable">$resolution</span> = <span class="hljs-string">"300"</span>; <span class="hljs-variable">$our_file</span> = <span class="hljs-string">"prvw.jpg"</span>; <span class="hljs-variable">$command</span>= <span class="hljs-string">"gs -dSAFER -dBATCH -dNOPAUSE -sDEVICE="</span> . <span class="hljs-variable">$our_format</span> . <span class="hljs-string">" "</span>; <span class="hljs-variable">$command</span> .= <span class="hljs-string">"-dTextAlphaBits="</span> . <span class="hljs-string">" -dGraphicsAlphaBits="</span> . . <span class="hljs-string">" "</span>; <span class="hljs-variable">$command</span> .= <span class="hljs-string">"-dFirstPage="</span> . <span class="hljs-variable">$prvw_page</span> . <span class="hljs-string">" -dLastPage="</span> . <span class="hljs-variable">$prvw_page</span> . <span class="hljs-string">" "</span>; <span class="hljs-variable">$command</span> .= <span class="hljs-string">"-r"</span> . <span class="hljs-variable">$resolution</span> . <span class="hljs-string">" "</span>; <span class="hljs-variable">$command</span> .= <span class="hljs-string">"-sOutputFile="</span> . <span class="hljs-variable">$our_file</span> . <span class="hljs-string">" '"</span> . <span class="hljs-variable">$file</span> . <span class="hljs-string">"'"</span>; <span class="hljs-keyword">echo</span> <span class="hljs-string">"Running command...\n"</span>; exec( <span class="hljs-variable">$command</span>, <span class="hljs-variable">$com_output</span>, <span class="hljs-variable">$ret_val</span> ); <span class="hljs-keyword">foreach</span>( <span class="hljs-variable">$com_output</span> <span class="hljs-keyword">as</span> <span class="hljs-variable">$line</span> ) { <span class="hljs-keyword">echo</span> <span class="hljs-variable">$line</span> . <span class="hljs-string">"\n"</span>; } <span class="hljs-keyword">if</span> ( !<span class="hljs-variable">$ret_val</span> ) { <span class="hljs-keyword">echo</span> <span class="hljs-string">"Preview created !!\n"</span>; } <span class="hljs-keyword">else</span> { <span class="hljs-keyword">echo</span> <span class="hljs-string">"Error while creating.\n"</span>; } } <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">__m<strong class="keylink">ai</strong>n__</span>() </span>{ <span class="hljs-keyword">global</span> <span class="hljs-variable">$arg</span>; <span class="hljs-variable">$inp_file</span> = <span class="hljs-variable">$arg</span>[<span class="hljs-number">1</span>]; <span class="hljs-keyword">if</span> ( my_pdf( <span class="hljs-variable">$inp_file</span> ) ) { <span class="hljs-comment">// Preview for the pdf</span> create_preview( <span class="hljs-variable">$inp_file</span> ); } <span class="hljs-keyword">else</span> { <span class="hljs-keyword">echo</span> <span class="hljs-string">"Thefile "</span> . <span class="hljs-variable">$inp_file</span> . <span class="hljs-string">" is not a valid PDF document.\n"</span>; } } __main__(); <span class="hljs-meta">?></span> </code></code>
程式碼執行從 _main_()
函數開始,它在命令列上取得 PDF 檔案並驗證其有效性。如果檔案有效,PHP 將執行 Ghostscript 指令。
輸出:
<code> <code class="language-bash hljs">$ php pdf_preview.php input.pdf Executing <span class="hljs-built_in">command</span>... GPL Ghostscript 9.22 (2022-08-05) Copyright (C) 2022 Artifex Software, Inc.All rights reserved. This software comes with NO WARRANTY: see the file PUBLIC <span class="hljs-keyword">for</span> details. Processing pages 1 through 1. Page 1 Preview created !! </code></code>
首先將所有 ImageMagick 二進位檔案安裝到您的系統中。執行以下命令以安裝 ImageMagick 相依性。
<code> <code class="language-bash hljs"><span class="hljs-variable">$sudo</span> dnf install <strong class="keylink">GC</strong>c php-devel php-pear </code></code>
執行以下命令以安裝 ImageMagick。
<code> <code class="language-bash hljs">$ sudo dnf install ImageMagick ImageMagick-devel </code></code>
然後,讓我們安裝 PHP 包裝器類別。
<code> <code class="language-bash hljs">$ sudo pecl install imagick $ sudo bash -c <span class="hljs-string">"echo "</span>extension=imagick.so<span class="hljs-string">" > /etc/php.d/imagick.ini"</span> </code></code>
對於在 LAMP 伺服器上使用它的用戶,您必須重新啟動 Apache 網路伺服器。
<code> <code class="language-bash hljs">$ sudo service <strong class="keylink">Http</strong>d restart </code></code>
至此,我們需要的一切都準備好了。我們現在可以使用早期的 PHP 腳本並編輯 create_preview()
函數。
<code> <code class="language-php hljs"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">create_preview</span> (<span class="hljs-params"> <span class="hljs-variable">$file</span> </span>) </span>{ <span class="hljs-variable">$output_format</span> = <span class="hljs-string">"jpeg"</span>; <span class="hljs-variable">$preview_page</span> = <span class="hljs-string">"1"</span>; <span class="hljs-variable">$resolution</span> = <span class="hljs-string">"300"</span>; <span class="hljs-variable">$output_file</span> = <span class="hljs-string">"imagick_preview.jpg"</span>; <span class="hljs-keyword">echo</span> <span class="hljs-string">"Fetching preview...\n"</span>; <span class="hljs-variable">$img_data</span> = <span class="hljs-keyword">new</span> Imagick(); <span class="hljs-variable">$img_data</span>->setResolution( <span class="hljs-variable">$resolution</span>, <span class="hljs-variable">$resolution</span> ); <span class="hljs-variable">$img_data</span>->readImage( <span class="hljs-variable">$file</span> . <span class="hljs-string">"["</span> . (<span class="hljs-variable">$preview_page</span> - <span class="hljs-number">1</span>) . <span class="hljs-string">"]"</span> ); <span class="hljs-variable">$img_data</span>->setImageFormat( <span class="hljs-variable">$output_format</span> ); file_put_contents( <span class="hljs-variable">$output_file</span>, <span class="hljs-variable">$img_data</span>, FILE_USE_INCLUDE_PATH ); } </code></code>
輸出結果:
<code> <code class="language-bash hljs">$ php pdf_preview.php input.pdf Fetching preview... </code></code>
這就是我們如何在 PHP 上從 PDF 文件建立預覽影像。這兩種方法具有相似的基本功能。您的選擇取決於您的喜好。
以上是在 PHP 中將 PDF 文件轉換為預覽影像的詳細內容。更多資訊請關注PHP中文網其他相關文章!