Using Gravity Forms, I have a php form with a PDF file upload field. We pass the form data (json) to a third party API. However, it is sending the PDF as a url to the file, and we need to actually pass the contents of the PDF encoded in the body.
We can use the following filter to modify the request data before the webhook is set, but I don't know how to encode the actual file contents:
add_filter( 'gform_webhooks_request_data', 'modify_data', 10, 4 );
function modify_data( $request_data, $feed, $entry, $form ){
$request_data = // do something
return $request_data;
}
How is it possible to read the file contents of the PDF and base64 encode it?
Thank you!
Related
I am trying to create a temporarily PDF file from raw PDF string.
This is my input, which is sent through an API (JSON):
{
"name": "pdffilename.pdf",
"content": "%PDF-1.2 [.......]%%EOF"
}
The "content" string is the actual PDF raw data string.
Now this is my controller, that handles the API request:
/**
* Function to convert a PDF file to text
*/
public function PDFtoText(Request $request)
{
$name = $request->name;
$content = $request->content;
//Save PDF file on the server (temp files).
$pdf = Storage::disk('local')->put('/temp_files/' . $name, $content);
return response()->json([
'result' => "Success"
], 200);
}
An actual file is created in the temp_files folder, with the name pdffilename.pdf. However I cannot open the file, as it says the file is "corrupt".
What am I doing wrong here?
the best way to save the content of the document or file is using the base64_encode function, this transform de content in base64 encode. Then when you need this content the unique process you need to do is to call the base64_decode function. I tried and works fine for me. Any questions only write me.
I've written a REST interface for my ownCloud app. I've method getFileFromRemote($path) which should return a JSON object with the file content.
Unfortunately this only works when the file that I've specified in $path is a plaintext file. When I try to call the method for an image or PDF the status code is 200 but the response is empty. For returning the file contents I use file_get_contents for retrieving the content.
Note: I know ownCloud has a WebDAV interface, but I want to solve this with REST only.
EDIT
This is the code server side (ownCloud):
public function synchroniseDown($path)
{
$this->_syncService->download(array($path));//get latest version
$content = file_get_contents($this->_homeFolder.urldecode($path));
return new DataResponse(['path'=>$path, 'fileContent'=>$content]);
}
The first line retrieves downloades the content on the ownCloud server and works completely.
You probably have to base64_encode your file content to make json_encode/decode handle it properly:
return new DataResponse([
'path'=>$path,
'fileContent' => base64_encode($content) // convert binary data to alphanum
]);
and then when receiving file via second side, you will have to always:
$fileContent = base64_decode($response['fileContent']);
It's only one, but ones of easiest way to handle that. Btw, sooner or later you will find that Mime-Type would be useful in response.
I have successfully implemented a form where I receive a JSON base64 encoded string from an external URL using curl. After which I decode the string and save the file as a pdf using php and then email the pdf to the user's email using swiftmailer.
The main section of the code after retrieving the JSON value stream
$jsonvalue = $json['value'];
$dcode = (base64_decode($jsonvalue));
file_put_contents($file, $dcode);
//Swift mailer email attachment code
After this I use php swiftmailer to email this file as an attachment.
Is this correct and the most efficient way of doing this?
Thanks for your time
1). Not sure you can directly save the content as a .pdf file. As far I know, you need to use a thirdparty library called tcpdf http://www.tcpdf.org/ for saving the file as PDF file.
2). You can use swift mailer to send the attachment with email.
In my webapp, I can generate pdf file from multiple odt files using gedooo on demand when the user go to this url: http://{base_url}/models/generer/{doc_id}, the modelsController generate the pdf and launch the download on the navigator.
Now I have to send to a distant application by webservice the content of pdf (like an attachment in email) from a model. The problem is to execute the pdf generation and integrate it in my request without store it on the server as file. I tried to do something like:
$pdf = new File('http://{base_url}/models/generer/{doc_id}');
$content = $pdf->read();
And I get an error. I am out of ideas, I need your help.
You can not transmit PDF (binary file).
if you want to transmit it in webservice, you have to convert it to base64
$file = file_get_contents('http://{base_url}/models/generer/{doc_id}');
$filedata = base64_encode($file);
now you can send this data in webservice
echo json_encode(array('file'=>$filedata));
I found the solution!
$this->requestAction($url);
that write the file in the folder of my webapp and
$content = file_get_contents($local_url)
I am having some issues with trying to get SwiftMailer to attach a file I have created with FPDF. Basically I have a page called createPDF.php that is dynamically generated based on the ID number in the URL. This page is set to output the PDF inline using $pdf->Output("filename.pdf",I);. What I want to do is to be able to attach this file to an email using SwiftMailer from another page simply by calling my createPDF.php?id=xxx link.
From the PHP page where I want to send the email from, everything works, except the attachment. It attaches something, but not what I want and it is not viewable in a PDF viewer on my local machine. The line specific to the attaching the file is:
->attach(Swift_Attachment::fromPath('createPDF.php?id=xxxx'))
This does not work, but surely, it must be possible without saving the file on my web server by FPDF.
Is this possible? If so, how?
Thanks!
The problem here is Swiftmailer gets the file contents, it does not execute your php file. So the contents of your PDF will the code that is in createPDF.php.
why cant you safe the file first? You should be able to safe it and delete it when your email is sent.
<?php
$id = "xxx";
$fileName = "tmp/".sha1(time()+mt_rand(0,99999999));
include "createPDF.php"; //saves it to $fileName
->attach(Swift_Attachment::fromFile( $fileName )->setFilename('blaha.pdf'));
unlink($fileName);
Ok, so I just figured this out.
Basically I made a new PHP file with the bulk of my createPDF.php file as a function and simply passed in two variables into the function as my $id and an $output variable. $output is simply the way that FPDF outputs the file — inline, etc... I then set the function to return the output of the FPDF. In my createPDF.php file I simply call my function passing in $id and 'I' as the variables so it displays the correct PDF inline in the browser.
In my sendEmail function I simply pass in $id and 'S' and set it to a variable $content, which I pass into SwiftMailer as an attachment.
Works great.
Thanks for your help!