wkhtmltopdf无需在php中创建文件

如何解决wkhtmltopdf无需在php中创建文件

我的 Drupal 中有 wkhtmltopdf 模块,它通过使用 'wkhtmltopdf --options URL filename.pdf' 函数运行 shell_exec 命令来生成 pdf 文件。

文件的输出很好,但我不想将pdf存储在文件系统中。我只想在浏览器上显示输出,以便用户可以选择是否下载。

就我搜索而言,我找不到一种方法可以将输出保存在缓冲区中,而不是将其存储在 pdf 文件中。 wkhtmltopdf不创建文件就可以生成pdf吗?

解决方法

GIF Demonstration (Over-engineered)

这是我为你写的一段过度设计的代码:)
它包括从功能到您可以测试的演示表单的所有内容。

我不保证此代码的稳定性,您可以随意查看 并对其进行修改以供您自己使用,但我不能保证 100% 的稳定性或安全性。

阅读有关 shell_exec 等函数的文档,以及为什么由于潜在的安全风险,这是一种不好的做法。

我的建议是用 C++ 编写一个 PHP 库并加载它并使用它 在 PHP 中。

我不确定 wkhtmltopdf 是否存在,如果我错了,有人在评论中纠正我。


更新 1

我在 http://ifconfig.me 上测试了这个脚本,它返回一个格式错误的 PDF 文档。
因此,您可能有 3 种选择,要么用 C++ 编写 PHP 库,等待有人提出更好的解决方案,要么将文件下载到 /tmp 中并使用 PHP 读取文件,然后将其删除。

GIF Demonstration (Simple)

代码(简单)

<?php

/**
 * --- DO NOT REMOVE THIS DOCBLOCK ---
 * @WebCrawlTrackingId cf9e8c67.3cb7269c.60b1d84b.5b2e5450
 */

/**
 * @file
 * Code for ni_wkhtmltopdf_simple function.
 * Includes a demonstration at the end.
 */

/**
 * Function that saves a PDF file
 * to a temporary directory and
 * returns it.
 * All of this by using wkhtmltopdf.
 *
 * @author t3ap0t@stackoverflow.com
 *
 * @param string $url
 *     URL to convert
 *
 * @param string $download
 *     Decide whether to download the
 *     file by specifying a filename
 *     or don't specify anything to
 *     display it in the browsers
 *     built-in PDF viewer.
 *
 * @return int|file
 *     Return (int) -1 if URL is empty
 *     Return (int) -2 if URL is not a string
 *     Return (int) -3 if URL is not a URL
 */
function ni_wkhtmltopdf_simple($url = "",$download = false) {
    // URL can't be empty
    if ($url == "") {
        return -1;
    }

    // URL must be a string
    if (gettype($url) !== "string") {
        return -2;
    }

    // Remove whitespace
    $url = trim($url);

    // Explode URL by ':' to Array
    $urla = explode(":",$url,2);

    // URL must be an actual URL
    if (strtolower(substr($urla[0],4)) !== "http" || substr($urla[1],2) !== "//") {
        return -3;
    }
    
    // Escape Shell Arguments
    $url = escapeshellarg($url);

    // Random file name
    $fname = "/tmp/" . bin2hex(random_bytes(10)) . ".pdf";

    // Generate a PDF file
    shell_exec("wkhtmltopdf \"$url\" \"$fname\"");

    // Load file
    $buffer = file_get_contents("$fname");
    
    // Delete the file after loading
    unlink("$fname");

    $buffsz = strlen($buffer);

    // Prepare headers
    header("Content-Type:application/pdf");

    if ($download) {
        $download = trim($download);
        header("Content-Disposition:attachment;filename=\"$download\"");
    } else {
        header("Content-Disposition:inline");
    }

    header("Content-Length:" . $buffsz);

    exit($buffer);
}

// Demonstrate ni_wkhtmltopdf_simple

// Are we getting the URL parameter?
if (isset($_GET["url"])) {
    // Convert array to string
    if (is_array($_GET["url"])) {
        $_GET["url"] = $_GET["url"][0];
    }
    
    // Remove whitespace
    $url = trim($_GET["url"]);

    // URL is empty so unset it
    if ($url == "") {
        unset($_GET["url"],$url);
        header("Location:" . basename(__FILE__));
    }

    // Get PDF output
    if (isset($url)) {
        ni_wkhtmltopdf_simple($url);
    }
} else {
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width">
    <title>PHP wkhtmltopdf_simple Demo (t3ap0t@stackoverflow.com)</title>
    <style>
        *{outline:0}
        html,body{
            zoom:1.25
        }
    </style>
</head>
<body>
    <form action="<?= basename(__FILE__) ?>" method="GET">
        <label for="url">URL:</label>
        <input id="url" name="url" type="text" value="https://" minlength="8" required autofocus />
        <button id="btn" type="submit">Generate PDF</button>

        <script type="text/javascript">
            function urlhandler(e) {
                // URL Value must begin with https://
                if (url.value.trim() == "") {
                    url.value = "https://" + url.value;
                }

                // Prevent removal of https://
                if (e.keyCode == 8 && url.value == "https://") {
                    e.preventDefault();
                }

                // Prevent Delete key
                if (e.keyCode == 46) {
                    e.preventDefault();
                }

                // Add https:// if it was removed during Paste operation
                if (url.value.substr(0,8).toLowerCase() !== "https://") {
                    url.value = "https://" + url.value;
                }
            }

            function btnhandler(e) {
                if (url.value.substr(0,8).toLowerCase() !== "https://") {
                    url.value = "https://" + url.value;
                }

                // Prevent submission of the form
                e.preventDefault();

                // Make sure we've provided a URL
                if (8 >= url.value.trim().length ||
                    url.value.trim()[9] == ".") {
                    alert("You must provide a URL.");
                    return;
                }
                
                // Automatically guess top-level domain
                if (url.value.trim().substr(-4,1) !== "." &&
                    url.value.trim().substr(-3,1) !== ".") {
                    url.value += ".com";
                }

                url.parentNode.submit();
            }
            
            // Event listeners
            url.addEventListener("keydown",function(e) {
                urlhandler(e);
            });
            
            url.addEventListener("onpaste",function(e) {
                urlhandler(e);
            });
            
            btn.addEventListener("click",function(e) {
               btnhandler(e);
            });
        </script>
    </form>
</body>
<?php
}
?>

代码(过度设计)

<?php

/**
 * --- DO NOT REMOVE THIS DOCBLOCK ---
 * @WebCrawlTrackingId fcc5094e.ccc3a1df.5eb4dbfa.6c3772e1
 */

/**
 * @file
 * Code for ni_wkhtmltopdf function.
 * Includes a demonstration at the end.
 */

/**
 * Function that returns a PDF file
 * from a URL using wkhtmltopdf.
 *
 * @author t3ap0t@stackoverflow.com
 *
 * @param string $url
 *     URL to convert
 *
 * @param string $https
 *     Ensures we're giving it HTTPS
 *
 * @param string $download
 *     Decide whether to download the
 *     file by specifying a filename
 *     or don't specify anything to
 *     display it in the browsers
 *     built-in PDF viewer.
 *
 * @param string $checkcmd
 *     Ensure we have all commands
 *     required to fulfil the operation.
 *
 *     * On Windows hosts these commands 
 *     can be acquired on using `scoop`.
 *
 * @param string $checkos
 *     Make sure we're running Linux.
 *
 *     * Optional if we have both commands
 *     available on a Windows host.
 *
 *
 * @return int|file
 *     Return (int) -1 if URL is empty
 *     Return (int) -2 if URL is not a string
 *     Return (int) -3 if URL is not a URL
 *     Return (int) -4 if protocol is not HTTPS
 *     Return (int) -5 if OS is not Linux
 *     Return (int) -6 if command wkhtmltopdf not found
 *     Return (int) -7 if command cat not found
 *     Return (int) -8 wkhtmltopdf returned nothing
 */
function ni_wkhtmltopdf($url = "",$https = false,$download = false,$checkcmd = true,$checkos = false) {
    // URL can't be empty
    if ($url == "") {
        return -1;
    }

    // URL must be a string
    if (gettype($url) !== "string") {
        return -2;
    }

    // Remove whitespace
    $url = trim($url);

    // Explode URL by ':' to Array
    $urla = explode(":",2) !== "//") {
        return -3;
    }

    // Optional: Make sure the URL is HTTPS (Secure)
    if ($https && strtolower(substr($url,8)) !== "https://") {
        return -4;
    }

    // Optional: Check operating system
    if ($checkos && strtolower(PHP_OS) !== "linux") {
        return -5;
    }

    // Optional: (Linux) Make sure the `wkhtmltopdf` command exists
    if ($checkcmd && !(`which wkhtmltopdf` > 0)) {
        return -6;
    }

    // Optional: (Linux) Make sure the `cat` command exists
    if ($checkcmd && !(`which cat` > 0)) {
        return -7;
    }

    // Clear URL to (hopefully) prevent RCE
    $rep = array(
        " "      => "%20","%20%20" => "","`"      => "%60",";"      => "%3B",":"      => "%3A",">"      => "%3E","<"      => "%3C","["      => "%5B","]"      => "%5D","{"      => "%7B","}"      => "%7D","("      => "%28",")"      => "%29","|"      => "%7C","$"      => "%24","&&"     => "%26%26",'"'      => "%22","\\"     => "%5C"
    );

    // Replace $a with $b inside URL
    foreach ($rep as $a => $b) {
        $url = str_replace($a,$b,$url);
    }

    unset($rep);

    // Generate a PDF file
    exec("wkhtmltopdf \"$url\" - | cat",$buffer);

    $buffer = implode("\n",$buffer);

    $buffsz = strlen($buffer);

    // Is buffer empty?
    if (0 >= $buffsz) {
        return -8;
    }

    // Prepare headers
    header("Content-Type:application/pdf");

    if ($download) {
        $download = trim($download);
        header("Content-Disposition:attachment;filename=\"$download\"");
    } else {
        header("Content-Disposition:inline");
    }

    header("Content-Length:" . $buffsz);

    exit($buffer);
}

// Demonstrate ni_wkhtmltopdf

// Are we getting the URL parameter?
if (isset($_GET["url"])) {
    // Convert array to string
    if (is_array($_GET["url"])) {
        $_GET["url"] = $_GET["url"][0];
    }
    
    // Remove whitespace
    $url = trim($_GET["url"]);

    // URL is empty so unset it
    if ($url == "") {
        unset($_GET["url"],$url);
        header("Location:" . basename(__FILE__));
    }

    // Get PDF output
    if (isset($url)) {
        ni_wkhtmltopdf($url);
    }
} else {
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width">
    <title>PHP wkhtmltopdf Demo (t3ap0t@stackoverflow.com)</title>
    <style>
        *{outline:0}
        html,function(e) {
               btnhandler(e);
            });
        </script>
    </form>
</body>
<?php
}
?>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res