PHP QR Code scan from PDF file - php

I have a PDF file with a QR Code. Iuploaded the file on the server folder named "tmp" and now I need to scan and convert this QR via PHP.
I found this library:
include_once('lib/QrReader.php');
$qrcode = new QrReader('/var/tmp/qrsample.png');
$text = $qrcode->text(); //return decoded text from QR Code
print $text;
But this works only for png/jpeg files.
Is there any way to scan PDF ? Is there any way to convert PDF to png only the time that I need ?
Thank you!

First, transform your PDF into an image with Imagick, then use your library to decode the QRcode from it:
include_once('lib/QrReader.php');
//the PDF file
$pdf = 'mypdf.pdf';
//retrieve the first page of the PDF as image
$image = new imagick($pdf.'[0]');
//pass it to the QRreader library
$qrcode = new QrReader($image, QrReader::SOURCE_TYPE_RESOURCE);
$text = $qrcode->text(); //return decoded text from QR Code
print $text;
//clear the resources used by Imagick
$image->clear();

You would want to convert the PDF to a supported image type, OR find a QR code reading library that supports PDFs. IMO the first option is easier. A quick search leads me to http://www.phpgang.com/how-to-convert-pdf-to-jpeg-in-php_498.html for a PDF -> img converter.

Presumably the QR code is embedded in the PDF as an image. You could use a command-line tool such as pdfimages to extract the image first, then run your QRReader library on the extracted image. You might need a bit of trial and error to establish which image is the QR code if there is more than one image in the PDF.
See extract images from PDF with PHP for more detail.

Related

Convert doc or docx to pdf use tcpdf not matched

I trying to convert file doc or docx to pdf but the result doesn't match with the origin file doc/docx and also there is no style in file pdf. I don't know why, because here i'm using tcpdf and phpword
this is my code to convert:
$filetarget = FileHelper::normalizePath($pathdirectory.'/'.$filename);
$objReader = \PhpOffice\PhpWord\IOFactory::createReader('Word2007');
$contents = $objReader->load($filetarget);
$tcpdfPath = Yii::getAlias('#baseApp') . '/vendor/tecnickcom/tcpdf';
\PhpOffice\PhpWord\Settings::setPdfRendererPath($tcpdfPath);
\PhpOffice\PhpWord\Settings::setPdfRendererName('TCPDF');
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($contents,'PDF');
$fileresult = str_replace('.docx', '.pdf', $filetarget);
$objWriter->save($fileresult);
$toPdf = FileHelper::normalizePath($fileresult);
this is part of result after converted from docx to pdf
and this is part of origin docx file
what's wrong with my code?
Unfortunately phpWord is very basic so for DocX to PDF output you can see there is no ability to preserve text or page breaks, nor support lists or export images.
For the current list of features see
https://phpword.readthedocs.io/en/latest/intro.html#writers
Since it runs OpenOffice as the converter you could try other PHP methods to run the conversion direct

How to fill image in pdf file using pdftk

This is my sample code.
I'm stuck on how to insert images into this.
I would be grateful for any help:
include('autoload.php');
use mikehaertl\pdftk\Pdf;
$dat = "review.png";
$pdf = new Pdf('Josco.pdf');
$pdf->fillForm(['signature'=>$dat ]);
$pdf->saveAs('my.pdf');
I don't know how to fill image in image type field. Please find attached
the pdf file for reference :
[1]: https://drive.google.com/file/d/1wz_mhdCFbG5o_1CgF6VlIJ4Gh70Ne8-E/view?usp=sharing
It is not possible to add image in a pdf using pdftk alone. A possibility would be to use tcpdf or fpdf to generate a pdf file of a blank page with the signature located where you want. Then to use the pdftk stamp function to stamp this generated file on the source pdf

Get content of PDF file in PHP

I have a FlipBook jquery page and too many ebooks(pdf format) to display on it. I need to keep these PDF's hidden so that I would like to get its content with PHP and display it with my FlipBook jquery page. (instead of giving whole pdf I would like to give it as parts).
Is there any way i can get whole content of PDF file with PHP?
I need to seperate them according to their pages.
You can use PDF Parser (PHP PDF Library) to extract each
and everything from PDF's.
PDF Parser Library Link: https://github.com/smalot/pdfparser
Online Demo Link: https://github.com/smalot/pdfparser/blob/master/doc/Usage.md
Documentation Link: https://github.com/smalot/pdfparser/tree/master/doc
Sample Code:
<?php
// Include Composer autoloader if not already done.
include 'vendor/autoload.php';
// Parse pdf file and build necessary objects.
$parser = new \Smalot\PdfParser\Parser();
$pdf = $parser->parseFile('document.pdf');
$text = $pdf->getText();
echo $text;
?>
Regarding another part of your Question:
How To Convert Your PDF Pages Into Images:
You need ImageMagick and GhostScript
<?php
$im = new imagick('file.pdf[0]');
$im->setImageFormat('jpg');
header('Content-Type: image/jpeg');
echo $im;
?>
The [0] means page 1.

PDF Image thumbnail preview in laravel 5.1?

I have file module, where I want to list all files,with thumbnail preview.
as like follows,
I am storing files in storage folder,which is not accessible via http.
So How can I provide thumbnail preview for docs especially image and PDF.
Is there any package availabe in laravel 5.1?
The preview image of a PDF file is usually the first page of the PDF file.
You can use ImageMagick to obtain that first page of the PDF file.
<?php
$imagick = new imagick('sample.pdf[0]'); // 0 specifies the first page of the pdf
$imagick->setImageFormat('jpg'); // set the format of the output image
header('Content-Type: image/jpeg'); // set the header for the browser to understand
echo $imagick; // display the image
?>
You can also output (save the contents of the image in a file) and store it under a thumbnail folder with the PDF name as the file name. like ( sample.jpg )
As for a solution based on laravel, I don't think laravel has any package that can do the same
Though the question was posted quite a long ago but I'm answering because it might helps others. There is a package for generating image from pdf. Pdf to Image. You should have Imagick and Ghostscript installed

PDF to Image Conversion - PHP

Note: PDF files would be uploaded by user.
I am making a document sharing website(pdf). I don't want users to be able to copy/select the pdf file text. I am looking for a nice PDF/Image Document viewer. Currently using the following method:
I am using imagemagick to convert PDF file to Jpeg. But the process is too slow even after disabling OpenMP. I was looking forward to convert full PDF to different image files but decided to just convert the first image to a thumbnail because the process is so slow.
I am using it on my local machine and it takes more than 30 seconds to convert 40-50 pages in good quality. So if there are pdf files more than 100 pages, this process would ruin the user experience.
Is there any way to convert PDF to Image files?
Also is there a way to let this process (pdf to image) happen asynchronously? Like the user filling out file details while the pdf is being uploaded and converted, something like YT videos?
Enter the command in exec() function, this will help you out
<?php
$pdf_url = 'testfile.pdf'; // Url of the PDF
$image_name = 'output.jpg'; // Set the image name
exec('convert "'.$pdf_url.'" -colorspace RGB -resize 800 "'.$image_name.'"', $output, $return_var);
?>
Hope this helps you
¿You use a linux sever?
Just use convert command and put [0] sufix to pdf file for save a cover:
exec("convert '/home/sample.pdf[0]' '/home/test.png'");

Categories