How to create file pdf file in google drive sdk using php - php

I am creating a pdf file using google drive sdk but I got an error when the file is created.. I cant download the file.
this is my php code in creating the pdf file:
$subcontent = "<h1>Hello World</h1><p>text here of some</p>";
$file = new Google_DriveFile();
$file->setTitle( $fileName );
$file->setDescription( $description );
$mkFile = $this->_service->files->insert($file, array('data' => $subcontent, 'mimeType' => 'application/pdf'));
the file is cannot be download or print..
the file also have an error displayed on the bottom saying
Opps! There was a problem loading more pages
can anyone help me in my case? I have been working on it in a week.. Thanks in advance, any help will be appreciated..

You seem to have made an assumption that Drive will convert your HTML file into a PDF file. It won't.
What you can do is ask Drive to convert your HTML to a Google doc, and then export that doc as PDF. Change your mime-type to text/html and add convert=true (see the library docs for how to do this).

Related

MPDF create read only pdf file?

I'm using mpdf to download an existing file like this-
$mpdf->Output('my_filename.pdf','D');
I need the file downloaded to be read only. Right now the downloaded files can be opened in word and edited, I wish to avoid that.
TL;DR :
I'm downloading an existing file from my system which my clients can edit by opening in word, need to avoid that.I can't have password protection for the files(client requirement)
Use the SetProtection() function as described here.
$filename = 'filename.pdf';
$html = 'Testing PDF protection.';
$mpdf = new mPDF('utf-8', 'A4-P');
$mpdf->SetProtection(array());
$mpdf->WriteHTML($html);
$mpdf->Output($filename,'D');

Embed ppt file in webpage

I currently have a problem to open a .ppt/.pptx file inside a webpage. I currently have all "uploaded" file in a folder and am able to open .html/.txt files in that folder, but not .ppt/.pptx. Whenever I try, a new window pops up and Windows Uploader starts to run.
<?php
$target_dir = "C:\Apache\htdocs\upload\\";
$target_file = $_FILES['file']['name'];
?>
<iframe src = "upload/"<?php echo $target_file; ?>" name = "iframe_s" id = "download" style = "display:none"></iframe>
Click here to view files
In the above code, I try to list all files in the folder, and the goal is a user can click on one of the file, then the file will open inside the webpage. The main problem I have right now is to have the program each individually attach different .ppt url to different files.
Thank you in advance!
For your question "How to upload to Google Docs".
My first Answer:Why don't you search properly???
If you still don't find it. Then, (taken from here)
You'd use the Google Documents API to upload them using a simple HTTP POST with the data. Here's an explanation how to upload and convert documents: http://code.google.com/apis/documents/docs/3.0/developers_guide_protocol.html#UploadingDocs
The link will provide you will examples and all you need to do what you were asking for.
To be more precise this is what you are looking for if you want to upload pdf's: http://code.google.com/apis/documents/docs/3.0/developers_guide_protocol.html#ResumableUploadPUT
Python Google Documents API guide
PHP Google Documents API guide

view pdf without downloading using dompdf

I generated a PDF using the dompdf library and used this:
$dompdf->stream('my.pdf',array('Attachment'=>0));
to force the browser to let the user view the PDF before downloading. But it's not working. A Google search isn't bringing up any answers either. Can anyone help?
Try using the below code. Let me know are you create data from form or html file data.
$dompdf->stream("dompdf_out.pdf", array("Attachment" => false));
exit(0);
If someone still looking for answer and it's still not resolved, try installing and turning on the PDF Viewer / PDF JS extension in the browser.
After adding parameter array(“Attachment”=>0) or array(“Attachment”=>false) to the stream and still not working. It's because your browser doesn't have a pdf reader available/activated. Installing and activating browser extension like PDF Viewer / PDFJS will force the browser to read and review the pdf.
Try this
$dompdf->stream("", array("Attachment" => false));
Instead of
$dompdf->stream();
It worked for me

CakePHP read PDF generated and send it as bytes

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)

Google Drive SDK (PHP Client Library) - Upload file but unreadable on the Google Drive UI

Here is my code to upload a file:
private function addFile(File $file, $folderProject, User $user) {
$fileToUpload = new Google_DriveFile();
$fileToUpload->setTitle($file->getName());
//set mime type
$fileToUpload->setMimeType($file->getMimeType());
//build parent and set id
$parent = new Google_ParentReference();
$parent->setId($folderProject->getId());
//set parent to file
$fileToUpload->setParents(array($parent));
$data = file_get_contents($file->getPath());
$createdFile = $this->service->files->insert($fileToUpload, array(
'data' => $data,
'mimeType' => $file->getMimeType(),
));
}
When I upload for example a docx using this PHP code the file append to be unreadable on the Google Drive website (it shows up the XML structure of the docx) and it is showing up like this in my Google Drive folder:
And when i try to upload a docx directly through Google Drive UI it showing up like this (what i want):
I'm pretty sure that the problem is coming from the fact the Google
Drive UI tries to open the file in a different way because i uploaded
it through my app, but i don't want that :)
(i have the same problem with other kind of files)
I tried to set the parameter 'convert' = true in the array but then i got this error during the upload:
..convert=true&uploadType=multipart&key=AI39si49a86oqv9fhb9yzXp7FCWL-cSqwFiClHZ‌​qS3Y3r9Hw8ujtSNlZEWZyFKBp_id7LUyFaZTJ97pMeb0XqHznycK7JJ9o_Q:
(500) Internal Error
Documentation: https://developers.google.com/drive/v2/reference/files/insert
=> But anyway i don't think to convert is the right solution, i just want to be able to see the content as if i uploaded the file through the Google Drive UI
If i open the docx from my Google Drive Sync folders and I edit and save it, I will be able to see it through the Google Drive UI (showing with the W logo)
I also getting the same problem.
But the difference is that , I am uploading pdf and able to view pdf in google drive UI.
I need to automatically convert it to google doc format by api code after adding new pdf.
If I change $mimeType = 'application/vnd.google-apps.document'; then got below error
https://www.googleapis.com/upload/drive/v2/files?convert=true&uploadType=multipart: (400) Bad Request
if I add optional parameter
$createdFile = $service->files->insert($file, array(
'data' => $data,
'mimeType' => $mimeType,
'convert' => TRUE
));
Then no change , only pdf upload no conversion takes place.
do u have any suggestion , where I need to exactly add for convert parameter ?

Categories