Laravel - edit pdf with FPDF with a soruce file from s3 - php

Im working on a Laravel backend and I need to edit a PDF file allocated in a s3 bucket. When I try opening the PDF with $pdf->setSourceFile($url) I get an error saying Given stream is not seekable!
I can get the file contents using Storage::disk(ENV('FILESYSTEM'))->url($url);, and return it to the front end and that works fine, so I know the path and the permissions are correct. I would like to the content to the setSourceFile($contents) method, intead of the url. is there any way to do this?
By the way, Im using the Laravel filesystem
Edit: I have tried copying the file from s3 to the server and then open it with PDF. I couldn't reach it, but I don't think that's a good way to do it.

First of all: You cannot edit a PDF document with FPDI
As the error message says, the source stream needs to be seekable which seems not be the case with the stream wrapper you're currently using.
You should download the file and create a reader instance of it:
$pdf->setSourceFile(StreamReader::createByString($pdfString));

Related

Laravel Streamed Response From Azure (Multiple Files)

I've been working on this setup but I cannot save the downloaded streamed response to my zip file. I am using ZipArchive package and currently when I use return on the one with the yellow arrow. It returns the correct pdf but when I try to put it in the zip folder. It doesn't recognize it as a pdf file and return null. I need to save multiple pdf file in the add Zip File but right now I'm trying with only one pdf for now.
Got it. What I did is convert it to a raw pdf file and read it using AddFromString.

How can display text content itself using PHP from Azure File with Azure storage account?

I have been installed the git lib from https://github.com/Azure/azure-storage-php .
I able to figure out the create file, download file, delete file.
But how can get the file content of the file and display on browser ?
Thank you.
I'm not that much knowledgeable about PHP but I believe the SDK method you would want to use is getBlob which reads a blob and returns an object of type GetBlobResult.
Contents of the blob can be read using getContentStream() method there.
If you're using Azure Files, more or less the approach would remain the same. You would be calling getFile method which reads a file and returns an object of type GetFileResult.
Contents of the file can be read using getContentStream() method there.

Is there a way to turn an FPDF output into a URL?

I created a pdf using FPDF in php. I want to have the website automatically upload this pdf to Google Drive through the API. However, seeing that this is not a file on the PC, can I turn the pdf into a url?
For instance, something like: http://www.pdf995.com/samples/pdf.pdf
Is this possible?
I think what you are looking for is
$fpdf->Output('F', 'Path to file');
It can be found in the documentation.
Save the file to a publicly accessible folder and echo the url to the user.

FPDF error: Unable to find "startxref" keyword

I have an app that renders PDFs with FPDF.
It was working fine and for some reasons I had to change the PDF files and now I get this error:
Unable to find "startxref" keyword
If I restore the original files, the same error happens.
How do I fix this error?
You simply have to pass a local path instead of an URI to setSourceFile().
The problems lies (sometimes) in the pdf compression.
If you are using linux you can use this to uncompress the pdf:
podofouncompress compressed.pdf decompressed.pdf
I faced this problem recently. I'm using TCPDF to merge uploaded pdf files.
Curiousilly something went wrong when importing some pdf files. Some cases, following error was printed:
Unable to find "startxref" keyword. in pdf_parser->_findXref()
So I tried to solve by reopen these pdfs files in Google Chrome and save it as PDF again. For my surprise, TCPDF worked! I still dont know how it can be but now I got a temporary solution.

PHP - Grab PDF with URL that does not have the .pdf extension

I'm using Filepicker.io to upload PDFs to my application. I have all those URLs and now I am trying to merge some of those PDFs using the PDF Tool Kit PHP library. It was not working for me so I ran some tests using the "file_exists" on PHP and it kept returning false.
I think this has to do with the fact that the URL does not have a ".pdf" extension at the end. This is what they look like: "https://www.filepicker.io/api/file/LCvbgpqEQLGwt8bfnqc1"
Does anyone know how I can pull the PDF using PHP in order to merge those files using the PDF Toolkit Library?
Thanks!
Alain F.
file_exists doesn't work with URLs, only with local files. Instead download the file to the temp dir using the copy command.
If the file can't be downloaded, the copy command will return false.
$exists = copy('https://www.filepicker.io/api/file/LCvbgpqEQLGwt8bfnqc1', '/tmp/example.pdf');
if (!$exists) throw new Exception("PDF could not be downloaded");
Use the downloaded file in the PDF Tool Kit.
EDIT: This does not solve this particular problem but does address the theory that it didn't work because "the URL does not have a ".pdf" extension at the end."
You can add things to the end of the filepicker URL with a trailing +
The following urls are equivalent:
https://www.filepicker.io/api/file/LCvbgpqEQLGwt8bfnqc1
https://www.filepicker.io/api/file/LCvbgpqEQLGwt8bfnqc1+name.pdf

Categories