/** * Set Container Defaults * * This is where we set all our defaults. If you need to customise this * container this is a good place to look to see what can be configured * and how to configure it. */ protected function setDefaults() { $this->file = $this->protect(function ($filePath) { return new SplFileInfo($filePath); }); $this->tempFile = $this->protect(function ($contents, $ext) { $file = new TempFile('GearsPdf', $ext); $file->setContents($contents); return $file; }); }
/** * Method: convertDoc * ========================================================================= * This is where we actually do some converting of docx to pdf. * We use the command line utility unoconv. Which is basically a slightly * fancier way of using OpenOffice/LibreOffice Headless. * * See: http://dag.wiee.rs/home-made/unoconv/ * * Parameters: * ------------------------------------------------------------------------- * - $docx: This must be an instance of ```SplFileInfo``` * pointing to the document to convert. * * Returns: * ------------------------------------------------------------------------- * void */ public function convertDoc(TempFile $docx) { if (!is_executable($this->binary)) { throw new RuntimeException('The unoconv command ("' . $this->binary . '") ' . 'was not found or is not executable by the current user! '); } // Check to see if the profile dir exists and is writeable if (is_dir($this->profile) && !is_writable($this->profile)) { throw new RuntimeException('If unoconv does not have permissions to the User ' . 'Profile directory ("' . $this->profile . '") the conversion ' . 'will fail!'); } // Build the unoconv cmd $cmd = 'export HOME=' . $this->profile . ' && ' . $this->binary . ' ' . '--stdout ' . '-f pdf ' . '"' . $docx->getPathname() . '"'; // Run the command $process = $this->process($cmd); $process->run(); // Check for errors $error = null; if (!$process->isSuccessful()) { $error = $process->getErrorOutput(); // NOTE: For some really odd reason the first time the command runs // it does not complete successfully. The second time around it // works fine. It has something to do with the homedir setup... if (Str::contains($error, 'Error: Unable to connect')) { $process->run(); if (!$process->isSuccessful()) { $error = $process->getErrorOutput(); } else { $error = null; } } if (!is_null($error)) { throw new RuntimeException($error); } } // Clean up after ourselves exec('rm -rf ' . $this->profile); // Return the pdf data return $process->getOutput(); }
/** * Method: convertDoc * ========================================================================= * This is where we actually do some converting of docx to pdf. * This converter uses the OpenOffice/LibreOffice Headless capabilities. * * Parameters: * ------------------------------------------------------------------------- * - $docx: This must be an instance of ```SplFileInfo``` * pointing to the document to convert. * * Returns: * ------------------------------------------------------------------------- * void */ public function convertDoc(TempFile $docx) { if (!is_executable($this->binary)) { throw new RuntimeException('The libreoffice command ("' . $this->binary . '") ' . 'was not found or is not executable by the current user! '); } // Check to see if the profile dir exists and is writeable if (is_dir($this->profile) && !is_writable($this->profile)) { throw new RuntimeException('If libreoffice does not have permissions to the User ' . 'Profile directory ("' . $this->profile . '") the conversion ' . 'will fail!'); } // Build the cmd to run $cmd = $this->binary . ' ' . '--headless ' . '-env:UserInstallation=file://' . $this->profile . ' ' . '--convert-to pdf:writer_pdf_Export ' . '--outdir "' . $this->output . '" ' . '"' . $docx->getPathname() . '"'; // Run the command $process = $this->process($cmd); $process->run(); // Check for errors if (!$process->isSuccessful()) { throw new RuntimeException($process->getErrorOutput()); } // Grab the generated pdf $pdf = file_get_contents($this->output . '/' . $docx->getBasename('.docx') . '.pdf'); // Clean up after ourselves exec('rm -rf ' . $this->profile); // Finally return the generated pdf return $pdf; }
/** * Method: convertDoc * ========================================================================= * This is where we actually do some converting of docx to pdf. * We use the command line utility unoconv. Which is basically a slightly * fancier way of using OpenOffice/LibreOffice Headless. * * See: http://dag.wiee.rs/home-made/unoconv/ * * Parameters: * ------------------------------------------------------------------------- * - $docx: This must be an instance of ```SplFileInfo``` * pointing to the document to convert. * * Returns: * ------------------------------------------------------------------------- * void */ public function convertDoc(TempFile $docx) { // Upload the document to google $gdoc = $this->service->files->insert($this->file, ['convert' => true, 'data' => $docx->getContents(), 'mimeType' => $this->mime, 'uploadType' => 'multipart']); // Now download the pdf $request = $this->request($gdoc->getExportLinks()['application/pdf']); $this->client->getAuth()->sign($request); $response = $this->client->getIo()->makeRequest($request); // Delete the uploaded file $this->service->files->delete($gdoc['id']); // Check for errors if ($response->getResponseHttpCode() != 200) { throw new RuntimeException($response->getResponseBody()); } // Return the pdf data return $response->getResponseBody(); }