/** * Save PHPExcel to file * * @param string $pFileName * @throws Exception */ public function save($pFilename = null) { // Open file global $cnf; $pFilename= $cnf['path']['Temp'] . $pFilename; $fileHandle = fopen($pFilename, 'w'); if ($fileHandle === false) { throw new Exception("Could not open file $pFilename for writing."); } // Fetch sheets $sheets = array(); if (is_null($this->_sheetIndex)) { $sheets = $this->_phpExcel->getAllSheets(); } else { $sheets[] = $this->_phpExcel->getSheet($this->_sheetIndex); } // PDF paper size $paperSize = 'A4'; // Create PDF $pdf = new FPDF('P', 'pt', $paperSize); // Loop all sheets foreach ($sheets as $sheet) { // PDF orientation $orientation = 'P'; if ($sheet->getPageSetup()->getOrientation() == PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE) { $orientation = 'L'; } // Start sheet $pdf->SetAutoPageBreak(true); $pdf->SetFont('Arial', '', 10); $pdf->AddPage($orientation); // Get worksheet dimension $dimension = explode(':', $sheet->calculateWorksheetDimension()); $dimension[0] = PHPExcel_Cell::coordinateFromString($dimension[0]); $dimension[0][0] = PHPExcel_Cell::columnIndexFromString($dimension[0][0]) - 1; $dimension[1] = PHPExcel_Cell::coordinateFromString($dimension[1]); $dimension[1][0] = PHPExcel_Cell::columnIndexFromString($dimension[1][0]) - 1; // Calculate column widths $sheet->calculateColumnWidths(); // Loop trough cells for ($row = $dimension[0][1]; $row <= $dimension[1][1]; $row++) { // Line height $lineHeight = 0; // Calulate line height for ($column = $dimension[0][0]; $column <= $dimension[1][0]; $column++) { $rowDimension = $sheet->getRowDimension($row); $cellHeight = PHPExcel_Shared_Drawing::pixelsToPoints( PHPExcel_Shared_Drawing::cellDimensionToPixels($rowDimension->getRowHeight()) ); if ($cellHeight <= 0) { $cellHeight = PHPExcel_Shared_Drawing::pixelsToPoints( PHPExcel_Shared_Drawing::cellDimensionToPixels($sheet->getDefaultRowDimension()->getRowHeight()) ); } if ($cellHeight <= 0) { $cellHeight = $sheet->getStyleByColumnAndRow($column, $row)->getFont()->getSize(); } if ($cellHeight > $lineHeight) { $lineHeight = $cellHeight; } } // Output values for ($column = $dimension[0][0]; $column <= $dimension[1][0]; $column++) { // Start with defaults... $pdf->SetFont('Arial', '', 10); $pdf->SetTextColor(0, 0, 0); $pdf->SetDrawColor(100, 100, 100); $pdf->SetFillColor(255, 255, 255); // Coordinates $startX = $pdf->GetX(); $startY = $pdf->GetY(); // Cell exists? $cellData = ''; if ($sheet->cellExistsByColumnAndRow($column, $row)) { if ($sheet->getCellByColumnAndRow($column, $row)->getValue() instanceof PHPExcel_RichText) { $cellData = $sheet->getCellByColumnAndRow($column, $row)->getValue()->getPlainText(); } else { if ($this->_preCalculateFormulas) { $cellData = PHPExcel_Style_NumberFormat::ToFormattedString( $sheet->getCellByColumnAndRow($column, $row)->getCalculatedValue(), $sheet->getstyle( $sheet->getCellByColumnAndRow($column, $row)->getCoordinate() )->getNumberFormat()->getFormatCode() ); } else { $cellData = PHPExcel_Style_NumberFormat::ToFormattedString( $sheet->getCellByColumnAndRow($column, $row)->getValue(), $sheet->getstyle( $sheet->getCellByColumnAndRow($column, $row)->getCoordinate() )->getNumberFormat()->getFormatCode() ); } } } // Style information $style = $sheet->getStyleByColumnAndRow($column, $row); // Cell width $columnDimension = $sheet->getColumnDimensionByColumn($column); if ($columnDimension->getWidth() == -1) { $columnDimension->setAutoSize(true); $sheet->calculateColumnWidths(false); } $cellWidth = PHPExcel_Shared_Drawing::pixelsToPoints( PHPExcel_Shared_Drawing::cellDimensionToPixels($columnDimension->getWidth()) ); // Cell height $rowDimension = $sheet->getRowDimension($row); $cellHeight = PHPExcel_Shared_Drawing::pixelsToPoints( PHPExcel_Shared_Drawing::cellDimensionToPixels($rowDimension->getRowHeight()) ); if ($cellHeight <= 0) { $cellHeight = $style->getFont()->getSize(); } // Column span? Rowspan? $singleCellWidth = $cellWidth; $singleCellHeight = $cellHeight; foreach ($sheet->getMergeCells() as $cells) { if ($sheet->getCellByColumnAndRow($column, $row)->isInRange($cells)) { list($first, ) = PHPExcel_Cell::splitRange($cells); if ($first == $sheet->getCellByColumnAndRow($column, $row)->getCoordinate()) { list($colSpan, $rowSpan) = PHPExcel_Cell::rangeDimension($cells); $cellWidth = $cellWidth * $colSpan; $cellHeight = $cellHeight * $rowSpan; } break; } } // Cell height OK? if ($cellHeight < $lineHeight) { $cellHeight = $lineHeight; $singleCellHeight = $cellHeight; } // Font formatting $fontStyle = ''; if ($style->getFont()->getBold()) { $fontStyle .= 'B'; } if ($style->getFont()->getItalic()) { $fontStyle .= 'I'; } if ($style->getFont()->getUnderline() != PHPExcel_Style_Font::UNDERLINE_NONE) { $fontStyle .= 'U'; } $pdf->SetFont('Arial', $fontStyle, $style->getFont()->getSize()); // Text alignment $alignment = 'L'; switch ($style->getAlignment()->getHorizontal()) { case PHPExcel_Style_Alignment::HORIZONTAL_CENTER: $alignment = 'C'; break; case PHPExcel_Style_Alignment::HORIZONTAL_RIGHT: $alignment = 'R'; break; case PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY: $alignment = 'J'; break; case PHPExcel_Style_Alignment::HORIZONTAL_LEFT: case PHPExcel_Style_Alignment::HORIZONTAL_GENERAL: default: $alignment = 'L'; break; } // Text color $pdf->SetTextColor( hexdec(substr($style->getFont()->getColor()->getRGB(), 0, 2)), hexdec(substr($style->getFont()->getColor()->getRGB(), 2, 2)), hexdec(substr($style->getFont()->getColor()->getRGB(), 4, 2)) ); // Fill color if ($style->getFill()->getFillType() != PHPExcel_Style_Fill::FILL_NONE) { $pdf->SetFillColor( hexdec(substr($style->getFill()->getStartColor()->getRGB(), 0, 2)), hexdec(substr($style->getFill()->getStartColor()->getRGB(), 2, 2)), hexdec(substr($style->getFill()->getStartColor()->getRGB(), 4, 2)) ); } // Border color $borders = ''; if ($style->getBorders()->getLeft()->getBorderStyle() != PHPExcel_Style_Border::BORDER_NONE) { $borders .= 'L'; $pdf->SetDrawColor( hexdec(substr($style->getBorders()->getLeft()->getColor()->getRGB(), 0, 2)), hexdec(substr($style->getBorders()->getLeft()->getColor()->getRGB(), 2, 2)), hexdec(substr($style->getBorders()->getLeft()->getColor()->getRGB(), 4, 2)) ); } if ($style->getBorders()->getRight()->getBorderStyle() != PHPExcel_Style_Border::BORDER_NONE) { $borders .= 'R'; $pdf->SetDrawColor( hexdec(substr($style->getBorders()->getRight()->getColor()->getRGB(), 0, 2)), hexdec(substr($style->getBorders()->getRight()->getColor()->getRGB(), 2, 2)), hexdec(substr($style->getBorders()->getRight()->getColor()->getRGB(), 4, 2)) ); } if ($style->getBorders()->getTop()->getBorderStyle() != PHPExcel_Style_Border::BORDER_NONE) { $borders .= 'T'; $pdf->SetDrawColor( hexdec(substr($style->getBorders()->getTop()->getColor()->getRGB(), 0, 2)), hexdec(substr($style->getBorders()->getTop()->getColor()->getRGB(), 2, 2)), hexdec(substr($style->getBorders()->getTop()->getColor()->getRGB(), 4, 2)) ); } if ($style->getBorders()->getBottom()->getBorderStyle() != PHPExcel_Style_Border::BORDER_NONE) { $borders .= 'B'; $pdf->SetDrawColor( hexdec(substr($style->getBorders()->getBottom()->getColor()->getRGB(), 0, 2)), hexdec(substr($style->getBorders()->getBottom()->getColor()->getRGB(), 2, 2)), hexdec(substr($style->getBorders()->getBottom()->getColor()->getRGB(), 4, 2)) ); } if ($borders == '') { $borders = 0; } if ($sheet->getShowGridlines()) { $borders = 'LTRB'; } // Image? $iterator = $sheet->getDrawingCollection()->getIterator(); while ($iterator->valid()) { if ($iterator->current()->getCoordinates() == PHPExcel_Cell::stringFromColumnIndex($column) . ($row + 1)) { try { $pdf->Image( $iterator->current()->getPath(), $pdf->GetX(), $pdf->GetY(), $iterator->current()->getWidth(), $iterator->current()->getHeight(), '', $this->_tempDir ); } catch (Exception $ex) { } } $iterator->next(); } // Print cell $pdf->MultiCell( $cellWidth, $cellHeight, $cellData, $borders, $alignment, ($style->getFill()->getFillType() == PHPExcel_Style_Fill::FILL_NONE ? 0 : 1) ); // Coordinates $endX = $pdf->GetX(); $endY = $pdf->GetY(); // Revert to original Y location if ($endY > $startY) { $pdf->SetY($startY); if ($lineHeight < $lineHeight + ($endY - $startY)) { $lineHeight = $lineHeight + ($endY - $startY); } } $pdf->SetX($startX + $singleCellWidth); // Hyperlink? if ($sheet->getCellByColumnAndRow($column, $row)->hasHyperlink()) { if (!$sheet->getCellByColumnAndRow($column, $row)->getHyperlink()->isInternal()) { $pdf->Link( $startX, $startY, $endX - $startX, $endY - $startY, $sheet->getCellByColumnAndRow($column, $row)->getHyperlink()->getUrl() ); } } } // Garbage collect! $sheet->garbageCollect(); // Next line... $pdf->Ln($lineHeight); } } // Document info $pdf->SetTitle($this->_phpExcel->getProperties()->getTitle()); $pdf->SetAuthor($this->_phpExcel->getProperties()->getCreator()); $pdf->SetSubject($this->_phpExcel->getProperties()->getSubject()); $pdf->SetKeywords($this->_phpExcel->getProperties()->getKeywords()); $pdf->SetCreator($this->_phpExcel->getProperties()->getCreator()); // Write to file fwrite($fileHandle, $pdf->output($pFilename, 'S')); // Close file fclose($fileHandle); }
$nb_eleve_aff = 1; // si la variable $gepiSchoolName est vide alors on cherche les informations dans la base if ( empty($gepiSchoolName) ) { $gepiSchoolName=getSettingValue('gepiSchoolName'); } // création du document $pdf->SetCreator($gepiSchoolName); // auteur du document $pdf->SetAuthor($gepiSchoolName); // mots clé $pdf->SetKeywords(''); // sujet du document $pdf->SetSubject('Bilan journalier des absences'); // titre du document $pdf->SetTitle('Bilan journalier des absences'); // méthode d'affichage du document à son ouverture $pdf->SetDisplayMode('fullwidth', 'single'); // compression du document $pdf->SetCompression(TRUE); // change automatiquement de page à 5mm du bas $pdf->SetAutoPageBreak(TRUE, 5); /* **************************** */ /* début de la boucle des pages */
/** * @brief Create PDF for a reference * * We create a simple cover page for the PDF. This page contains basic bibliographic metadata * for the PDF, in order for Mendeley to process the PDF correctly. In response to my discovery * that Mendeley doesn't accept all XMP (Ticket#2010040110000015) support@mendeley.com replied that * they have some heuristic tests to see if the metadata is valid, such as whether the information * about the title and authors occurs on the first of the PDF. * * * @param reference_id Reference id * @param pdf_filename Full path of PDF file to create * */ function pdf_create($reference_id, $pdf_filename) { global $config; // Get reference $reference = db_retrieve_reference($reference_id); // Get tags $tags = pdf_tags($reference->reference_id, 10); // Paper size // A4 = 210 x 297 $paper_width = 210; // mm $paper_height = 297; // mm $margin = 10; //---------------------------------------------------------------------------------------------- // PDF $pdf = new FPDF('P', 'mm', 'A4'); //$pdf = PDF_Rotate('P', 'mm', 'A4'); //---------------------------------------------------------------------------------------------- // Basic metadata (e.g., that displayed by Mac OS X Preview) $pdf->SetTitle($reference->title, true); // true means use UTF-8 $pdf->SetAuthor(reference_authors_to_text_string($reference), true); // true means use UTF-8 if (count($tags) > 0) { $pdf->SetKeywords(join(", ", $tags), true); } //---------------------------------------------------------------------------------------------- // Cover page (partly to ensure Mendeley accepts XMP metadata) $pdf->AddPage(); // Title $pdf->SetFont('Arial', '', 24); $pdf->SetXY($margin, $margin); $pdf->Write(10, utf8_decode($reference->title)); // Authors $y = $pdf->GetY(); $pdf->SetXY($margin, $y + 16); $pdf->SetFont('Arial', 'B', 16); $pdf->Write(6, utf8_decode(reference_authors_to_text_string($reference))); // Citation $y = $pdf->GetY(); $pdf->SetXY($margin, $y + 10); $pdf->SetFont('Arial', 'I', 12); $pdf->Write(6, utf8_decode($reference->secondary_title)); $pdf->SetFont('Arial', '', 12); $pdf->Write(6, ' ' . $reference->volume); if (isset($reference->issue)) { $pdf->Write(6, '(' . $reference->issue . ')'); } $pdf->Write(6, ':' . $reference->spage); if (isset($reference->epage)) { $pdf->Write(6, '-' . $reference->epage); } $pdf->Write(6, ' (' . $reference->year . ')'); // URL $url = $config['web_root'] . 'reference/' . $reference->reference_id; $pdf->Write(6, ' '); $pdf->SetTextColor(0, 0, 255); $pdf->SetFont('', 'U'); $pdf->Write(6, $url, $url); $pdf->SetFont('', ''); $pdf->SetTextColor(0, 0, 0); //---------------------------------------------------------------------------------------------- // If we add taxon names as keywords if (count($tags) > 0) { $keywords = "Keywords: " . join("; ", $tags); $y = $pdf->GetY(); $pdf->SetXY($margin, $y + 10); $pdf->Write(6, $keywords); } //---------------------------------------------------------------------------------------------- // Footer giving credit to BHL $y = $paper_height; $y -= $margin; $y -= 40; $pdf->Image($config['web_dir'] . '/images/cc/cc.png', 10, $y, 10); $pdf->Image($config['web_dir'] . '/images/cc/by.png', 20, $y, 10); $pdf->Image($config['web_dir'] . '/images/cc/nc.png', 30, $y, 10); $pdf->SetXY(10, $y + 10); $pdf->SetFont('Arial', '', 10); $pdf->SetTextColor(0, 0, 0); $pdf->Write(6, 'Page images from the Biodiversity Heritage Library, '); //Then put a blue underlined link $pdf->SetTextColor(0, 0, 255); $pdf->SetFont('', 'U'); $pdf->Write(6, 'http://www.biodiversitylibrary.org/', 'http://www.biodiversitylibrary.org/'); $pdf->SetFont('', ''); $pdf->SetTextColor(0, 0, 0); $pdf->Write(6, ', made available under a Creative Commons Attribution-Noncommercial License '); $pdf->SetTextColor(0, 0, 255); $pdf->SetFont('', 'U'); $pdf->Write(6, 'http://creativecommons.org/licenses/by-nc/2.5/', 'http://creativecommons.org/licenses/by-nc/2.5/'); //---------------------------------------------------------------------------------------------- // Add BHL page scans $pages = bhl_retrieve_reference_pages($reference_id); foreach ($pages as $page) { $image = bhl_fetch_page_image($page->PageID); $page_width = $paper_width; $page_height = $paper_height; $page_width -= 2 * $margin; $page_height -= 2 * $margin; // Fit to page $img_height = $image->height; $img_width = $image->width; $w_scale = $page_width / $img_width; $h_scale = $page_height / $img_height; $scale = min($w_scale, $h_scale); $img_height *= $scale; $img_width *= $scale; $pdf->AddPage(); $x_offset = ($paper_width - $img_width) / 2.0; $y_offset = ($paper_height - $img_height) / 2.0; $pdf->Image($image->file_name, $x_offset, $y_offset, $img_width); } $pdf->Output($pdf_filename, 'F'); pdf_add_xmp($reference, $pdf_filename, $tags); }
/** * * Wrapper to solve utf-8 issues. * * @param string $keywords * * @param bool $isUTF8 Defaults to TRUE. * * @return void * */ public function SetKeywords($keywords, $isUTF8 = TRUE) { parent::SetKeywords($keywords, $isUTF8); }