Ejemplo n.º 1
0
 public function testConvert()
 {
     $html = file_get_contents('./tests/templates/PhantomJs.html');
     $result = Gears\Pdf::convert($html, './tests/output/PhantomJsConvert.pdf');
     $this->assertInstanceOf('SplFileInfo', $result);
     $this->assertFileExists('./tests/output/PhantomJsConvert.pdf');
     $text = Str::s($this->pdfBox->textFromPdfFile('./tests/output/PhantomJsConvert.pdf'))->to('ascii');
     $this->assertTrue($text->contains('Iamthecoverpage.'));
     $this->assertTrue($text->contains('B15/15'));
 }
Ejemplo n.º 2
0
 public function testCloneRow()
 {
     $document = new Gears\Pdf('./tests/templates/CloneRow.docx');
     $document->converter = $this->converter;
     $document->cloneRow('rowValue', 10);
     $document->setValue('rowValue_1', 'Sun');
     $document->setValue('rowValue_2', 'Mercury');
     $document->setValue('rowValue_3', 'Venus');
     $document->setValue('rowValue_4', 'Earth');
     $document->setValue('rowValue_5', 'Mars');
     $document->setValue('rowValue_6', 'Jupiter');
     $document->setValue('rowValue_7', 'Saturn');
     $document->setValue('rowValue_8', 'Uranus');
     $document->setValue('rowValue_9', 'Neptun');
     $document->setValue('rowValue_10', 'Pluto');
     $document->setValue('rowNumber_1', '1');
     $document->setValue('rowNumber_2', '2');
     $document->setValue('rowNumber_3', '3');
     $document->setValue('rowNumber_4', '4');
     $document->setValue('rowNumber_5', '5');
     $document->setValue('rowNumber_6', '6');
     $document->setValue('rowNumber_7', '7');
     $document->setValue('rowNumber_8', '8');
     $document->setValue('rowNumber_9', '9');
     $document->setValue('rowNumber_10', '10');
     $document->cloneRow('userId', 3);
     $document->setValue('userId_1', '1');
     $document->setValue('userFirstName_1', 'James');
     $document->setValue('userName_1', 'Taylor');
     $document->setValue('userPhone_1', '+1 428 889 773');
     $document->setValue('userId_2', '2');
     $document->setValue('userFirstName_2', 'Robert');
     $document->setValue('userName_2', 'Bell');
     $document->setValue('userPhone_2', '+1 428 889 774');
     $document->setValue('userId_3', '3');
     $document->setValue('userFirstName_3', 'Michael');
     $document->setValue('userName_3', 'Ray');
     $document->setValue('userPhone_3', '+1 428 889 775');
     $document->save('./tests/output/UnoconvoCloneRow.pdf');
     $text = Str::s($this->pdfBox->textFromPdfFile('./tests/output/UnoconvoCloneRow.pdf'))->to('ascii');
     $this->assertTrue($text->contains('Value 1: Sun'));
     $this->assertTrue($text->contains('Value 2: Mercury'));
     $this->assertTrue($text->contains('Value 3: Venus'));
     $this->assertTrue($text->contains('Value 4: Earth'));
     $this->assertTrue($text->contains('Value 5: Mars'));
     $this->assertTrue($text->contains('Value 6: Jupiter'));
     $this->assertTrue($text->contains('Value 7: Saturn'));
     $this->assertTrue($text->contains('Value 8: Uranus'));
     $this->assertTrue($text->contains('Value 9: Neptun'));
     $this->assertTrue($text->contains('Value 10: Pluto'));
     $this->assertTrue($text->contains("1 Name TaylorFirst name JamesPhone +1 428 889 773"));
     $this->assertTrue($text->contains("2 Name BellFirst name RobertPhone +1 428 889 774"));
     $this->assertTrue($text->contains("3 Name RayFirst name MichaelPhone +1 428 889 775"));
 }
Ejemplo n.º 3
0
 /**
  * Generates the PDF from the HTML File
  *
  * @return PDF Bytes
  */
 public function generate()
 {
     // Inject our print framework
     if ($this->printFramework !== false) {
         $this->document->setContents(Str::s($this->document->getContents())->replace('</head>', $this->printFramework . '</head>'));
     }
     // Create a new temp file for the generated pdf
     $output_document = $this->tempFile();
     // Build the cmd to run
     $cmd = $this->binary . ' ' . $this->runner . ' ' . '--url "file://' . $this->document->getPathname() . '" ' . '--output "' . $output_document->getPathname() . '" ';
     if (isset($this->paperSize['width'])) {
         $cmd .= '--width "' . $this->paperSize['width'] . '" ';
     }
     if (isset($this->paperSize['height'])) {
         $cmd .= '--height "' . $this->paperSize['height'] . '" ';
     }
     // Run the command
     $process = $this->process($cmd);
     $process->run();
     // Check for errors
     if (!$process->isSuccessful()) {
         throw new RuntimeException($process->getErrorOutput());
     }
     // Return the pdf
     return $output_document->getContents();
 }
Ejemplo n.º 4
0
Archivo: Pdf.php Proyecto: lucagtc/pdf
 /**
  * Saves the generated PDF.
  *
  * We call the backend class to generate the PDF for us.
  * Then we attempt to save those bytes to a permanent location.
  *
  * @param string $path If not supplied we will create the PDF in the name
  *                     folder as the source document with the same filename.
  *
  * @return SplFileInfo
  */
 public function save($path = null)
 {
     $pdf = $this->backend->generate();
     // If no output path has been supplied save the file
     // in the same folder as the original template.
     if (is_null($path)) {
         if (is_null($this->originalDocument)) {
             // This will be thrown when someone attemtps to use
             // the save method when they have supplied a HTML string.
             throw new RuntimeException('You must supply a path for us to save the PDF!');
         }
         $ext = $this->originalDocument->getExtension();
         $path = Str::s($this->originalDocument->getPathname());
         $path = $path->replace('.' . $ext, '.pdf');
     }
     // Save the pdf to the output path
     if (@file_put_contents($path, $pdf) === false) {
         throw new RuntimeException('Failed to write to file "' . $path . '".');
     }
     // Return the location of the saved pdf
     return $this->file($path);
 }
Ejemplo n.º 5
0
 /**
  * Method: getStartAndEndNodes
  * =========================================================================
  * Searches the xml with the given blockname and returns
  * the corresponding start and end nodes.
  * 
  * Parameters:
  * -------------------------------------------------------------------------
  *  - $xml: An instance of ```SimpleXMLElement```
  *  - $blockname: The name of the block to find.
  * 
  * Returns:
  * -------------------------------------------------------------------------
  * array
  */
 protected function getStartAndEndNodes($xml, $blockname)
 {
     // Assume the nodes don't exist
     $startNode = false;
     $endNode = false;
     // Search for the block start and end tags
     foreach ($xml->xpath('//w:t') as $node) {
         if (Str::contains($node, $this->normaliseStartTag($blockname))) {
             $startNode = $node;
             continue;
         }
         if (Str::contains($node, $this->normaliseEndTag($blockname))) {
             $endNode = $node;
             break;
         }
     }
     // Bail out if we couldn't find anything
     if ($startNode === false || $endNode === false) {
         return false;
     }
     // Find the parent <w:p> node for the start tag
     $node = $startNode;
     $startNode = null;
     while (is_null($startNode)) {
         $node = $node->xpath('..')[0];
         if ($node->getName() == 'p') {
             $startNode = $node;
         }
     }
     // Find the parent <w:p> node for the end tag
     $node = $endNode;
     $endNode = null;
     while (is_null($endNode)) {
         $node = $node->xpath('..')[0];
         if ($node->getName() == 'p') {
             $endNode = $node;
         }
     }
     // Return the start and end node
     return [$startNode, $endNode];
 }
Ejemplo n.º 6
0
 /**
  * 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();
 }