PDFTK with php://memory


PDFTK with php://memory

问题:是否可以在execpassthru命令上使用 php://memory

我可以毫无问题地在 exec 或直通中使用 php 变量,但我在 php://memory

背景:我正在尝试使用 PDFTK 消除我所有的临时 pdf 文件编写。
1(我正在编写一个临时的fdf文件
2( 使用 #1
填写临时 PDF 文件3(对所有PDF
重复#1和#24(将所有PDF合并在一起。

这目前有效 - 但它会创建大量文件,并且是瓶颈。

我想通过使用虚拟文件 php://memory 来加快pdftk的速度

首先,我正在尝试虚拟化#1中使用的fdf文件。
仅回答这个问题就足以获得"正确答案"。   :)

代码如下:

$fdf = 'fdf file contents here';
$tempFdfVirtual= fopen("php://memory", 'r+');
if(  $tempFdfVirtual ) {
  fwrite(  $tempFdfVirtual, $fdf);
} else {
    echo "Failure to open temporary fdf file";
    exit;
}
rewind( $tempFdfVirtual);
$url = "unfilled.pdf";
$temppdf_fn = "output.pdf"; 
$command = "pdftk $url fill_form  $tempFdfVirtual output $temppdf_fn flatten"; 
$error="";   
exec( $command, $error );
if ($error!="") {
    $_SESSION['err'] = $error;       
} else {
    $_SESSION['err'] = 0;
}

我收到错误代码#1。 如果我做一个stream_get_contents($tempFdfVirtual(,它会显示内容。

感谢您的观看!

php://memoryphp://temp(实际上任何文件描述符(仅适用于当前运行的 PHP 进程。此外,$tempFdfVirtual是一个资源句柄,因此将其放在字符串中是没有意义的。

应将数据从资源句柄传递到进程,通过其标准输入。您可以使用 proc-open 来执行此操作,这使您可以比 exec 更好地控制子进程的输入和输出。

请注意,由于某种原因,您无法将"php://memory"文件描述符传递给进程。PHP 会抱怨:

警告:proc_open((:不能将 MEMORY 类型的流表示为文件描述符

请改用php://temp,它应该是完全相同的,只是一旦流变得足够大,它将使用临时文件。

这是一个经过测试的示例,说明了使用 proc_open() 的代码的一般模式。这应该包含在函数或其他抽象中:

$testinput = "THIS IS A TEST STRING'n";
$fp = fopen('php://temp', 'r+');
fwrite($fp, $testinput);
rewind($fp);
$cmd = 'cat';
$dspec = array(
    0 => $fp,
    1 => array('pipe', 'w'),
);
$pp = proc_open($cmd, $dspec, $pipes);
// busywait until process is finished running.
do {
    usleep(10000);
    $stat = proc_get_status($pp);
} while($stat and $stat['running']);
if ($stat['exitcode']===0) {
    // index in $pipes will match index in $dspec
    // note only descriptors created by proc_open will be in $pipes
    // i.e. $dspec indexes with an array value.
    $output = stream_get_contents($pipes[1]);
    if ($output == $testinput) {
        echo "TEST PASSED!!";
    } else {
        echo "TEST FAILED!! Output does not match input.";
    }
} else {
    echo "TEST FAILED!! Process has non-zero exit status.";
}
// cleanup
// close pipes first, THEN close process handle.
foreach ($pipes as $pipe) {
    fclose($pipe);
}
// Only file descriptors created by proc_open() will be in $pipes.
// We still need to close file descriptors we created ourselves and
// passed to it.
// We can do this before or after proc_close().
fclose($fp);
proc_close($pp);

特定于您使用 PDFTK 的未经测试的示例:

// Command takes input from STDIN
$command = "pdftk unfilled.pdf fill_form - output tempfile.pdf flatten"; 
$descriptorspec = array(
    0 => $tempFdfVirtual, // feed stdin of process from this file descriptor
//    1 => array('pipe', 'w'), // Note you can also grab stdout from a pipe, no need for temp file
);
$prochandle = proc_open($command, $descriptorspec, $pipes);
// busy-wait until it finishes running
do {
    usleep(10000);
    $stat = proc_get_status($prochandle);
} while ($stat and $stat['running']);
if ($stat['exitcode']===0) {
    // ran successfully
    // output is in that filename
    // or in the file handle in $pipes if you told the command to write to stdout.
}
// cleanup
foreach ($pipes as $pipe) {
   fclose($pipe);
}
proc_close($prochandle);

这不仅仅是你正在使用php://memory,它是任何文件句柄。文件句柄仅存在于当前进程。出于所有意图和目的,您从 fopen 返回的句柄不能转移到脚本之外的任何其他位置。

只要您使用外部应用程序,您就几乎无法使用临时文件。您唯一的其他选择是尝试将数据传递给 stdin上的 pdftk,并在 stdout 上检索输出(如果它支持(。据我所知,通过这种访问其描述符(stdin/stdout(来调用外部进程的唯一方法是使用proc_系列函数,特别是proc_open。