0
votes

I am trying to generate a PDF using TCPDF/TCPDI but then flatten the PDF to become an image-based pdf. The result would be the PDF would not be selectable and ideally would be to stop Adobe allowing text editing.

From what I have read, it isn't something that is possible with just TCPDF however, I have recently come across a website that does exactly this and states it was generated by FPDF.

I have managed to get the results by passing it through TCPDF, then using Ghostscript pdfimage24 afterwards.

$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

$pdfdata = file_get_contents('docs/original.pdf');

$pagecount = $pdf->setSourceData($pdfdata);
for ($i = 1; $i <= $pagecount; $i++) {
    $tplidx = $pdf->importPage($i);

    $pdf->AddPage();
    $pdf->useTemplate($tplidx);

    // PDF INFO TO MERGE HERE

}

$pdf_out = $pdf->Output($before, 'F');

exec('gs -q -dNOPAUSE -sDEVICE=pdfimage24 -r300 -dDownScaleFactor=3 -sOutputFile=docs/after.pdf docs/before.pdf -c quit');

As I am new to PHP, my question would be is this the right away to go about this, could this way hard on performance and is there any way of getting the result in TCPDF which is based on FPDF to flatten an image in this way.

Thanks in advance.

1

1 Answers

0
votes

I'm not clear why you've tagged this with Ghostscript, GS appears to be behaving exactly as you asked it to.

You could prevent copying text (if that's your real goal) by choosing the pdfwrite device and -dNoOutputFonts rather than rendering to an image. It'll still be larger than using the original file with fonts, but you certainly won't be able to copy/edit the text because there won't be any (all the 'text' is drawn as vector paths instead)

Turning a PDF file into an image is going to produce a file which is large and doesn't scale well, or if you keep the resolution low to keep a small file, won't be legible. Unless you particularly want the PDF to consist of nothing but a bitmap for some reason, I wouldn't do that.