I generate Excel reports by loading Excel sheets, that I generated with Excel and fill in some data by PHP code.
Now, I wanted to convert those Excel sheets to PDF and because I saw that the PHPExcel 1.7.x supports creating PDF I thought: perfect, I do not have to go over a OpenOffice Engine, because that engine was not useful to generate PDFs for Web-Users in real-time.
But: Beside I did not have any chance to use the PDF writer from PHPExcel to write a loaded Excel template. It was not even possible to load and write delivered xls files.
I tried to build up the excel by PHP code only and drop the excel template: the excel-to-pdf already fails with the simplest generated excel that merges two cells.
Is this writer completely unusable for such a task?
Appendix, 2013-06-09: Sorry, this could be understood wrong: It could be all my fault or I just did not understand the core principles.
The following simple excel generation code fails if it comes to output the pdf:
<?php
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');
if (PHP_SAPI == 'cli')
die('This example should only be run from a Web Browser');
/** Include PHPExcel */
require_once 'app/_includes/PHPExcel_1.7/PHPExcel.php';
$rendererName = PHPExcel_Settings::PDF_RENDERER_TCPDF;
$rendererLibrary = 'tcpdf';
$rendererLibraryPath = '/inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/'. $rendererLibrary;
if (!PHPExcel_Settings::setPdfRenderer(
$rendererName,
$rendererLibraryPath
)) {
die(
'NOTICE: Please set the $rendererName and $rendererLibraryPath values' .
'<br />' .
'at the top of this script as appropriate for your directory structure'
);
}
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("Company X")
->setDescription("Test document for PDF, generated using PHP classes.");
$objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
// Add some data
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Date')
->setCellValue('B1', 'Time')
->setCellValue('B2', 'Start')
->setCellValue('C2', 'End');
// This line generated errors inside the output pdf -> corrupts PDF
$objPHPExcel->setActiveSheetIndex(0)->mergeCells('B1:C1');
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(8);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(14);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(4);
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('Simple');
$objPHPExcel->getActiveSheet()->setShowGridLines(false);
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
// Redirect output to a client.s web browser (PDF)
header('Content-Type: application/pdf');
header('Content-Disposition: attachment;filename="p1.pdf"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
$objWriter->save('php://output');
exit;
Appendix, 2013-06-09: This is the beginning of the generated PDF file:
Notice: Array to string conversion in /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/HTML.php on line 1236
Call Stack:
0.0010 257896 1. {main}() /inet/xxx/HTTP/devel/p1.php:0
0.0243 7798872 2. PHPExcel_Writer_PDF->save() /inet/xxx/HTTP/devel/p1.php:62
0.0243 7799224 3. PHPExcel_Writer_PDF->__call() /inet/xxx/HTTP/devel/p1.php:62
0.0243 7799728 4. call_user_func_array() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/PDF.php:87
0.0243 7800064 5. PHPExcel_Writer_PDF_tcPDF->save() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/PDF.php:87
0.0309 8678376 6. PHPExcel_Writer_HTML->generateSheetData() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/PDF/tcPDF.php:119
0.0313 8683176 7. PHPExcel_Writer_HTML->_generateRow() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/HTML.php:436
Warning: Illegal string offset 'width' in /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/HTML.php on line 1257
Call Stack:
0.0010 257896 1. {main}() /inet/xxx/HTTP/devel/p1.php:0
0.0243 7798872 2. PHPExcel_Writer_PDF->save() /inet/xxx/HTTP/devel/p1.php:62
0.0243 7799224 3. PHPExcel_Writer_PDF->__call() /inet/xxx/HTTP/devel/p1.php:62
0.0243 7799728 4. call_user_func_array() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/PDF.php:87
0.0243 7800064 5. PHPExcel_Writer_PDF_tcPDF->save() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/PDF.php:87
0.0309 8678376 6. PHPExcel_Writer_HTML->generateSheetData() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/PDF/tcPDF.php:119
0.0313 8683176 7. PHPExcel_Writer_HTML->_generateRow() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/HTML.php:436
Warning: Invalid argument supplied for foreach() in /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/HTML.php on line 1315
Call Stack:
0.0010 257896 1. {main}() /inet/xxx/HTTP/devel/p1.php:0
0.0243 7798872 2. PHPExcel_Writer_PDF->save() /inet/xxx/HTTP/devel/p1.php:62
0.0243 7799224 3. PHPExcel_Writer_PDF->__call() /inet/xxx/HTTP/devel/p1.php:62
0.0243 7799728 4. call_user_func_array() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/PDF.php:87
0.0243 7800064 5. PHPExcel_Writer_PDF_tcPDF->save() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/PDF.php:87
0.0309 8678376 6. PHPExcel_Writer_HTML->generateSheetData() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/PDF/tcPDF.php:119
0.0313 8683176 7. PHPExcel_Writer_HTML->_generateRow() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/HTML.php:436
0.0320 8701296 8. PHPExcel_Writer_HTML->_assembleCSS() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/HTML.php:1267
%PDF-1.7
Appendix, 2013-06-10: I tried the 21pdf.php example, with tcpdf. It shows me some warnings but the pdf is generated. These is the php output:
21:55:52 Create new PHPExcel object
21:55:52 Set document properties
21:55:52 Add some data
21:55:52 Add comments
21:55:52 Add rich-text string
21:55:52 Merge cells
21:55:52 Protect cells
21:55:52 Set cell number formats
21:55:52 Set column widths
21:55:52 Set fonts
21:55:52 Set alignments
21:55:52 Set thin black border outline around column
21:55:52 Set thick brown border outline around Total
21:55:52 Set fills
21:55:52 Set style for header row using alternative method
21:55:52 Unprotect a cell
21:55:52 Add a hyperlink to the sheet
21:55:52 Add a drawing to the worksheet
21:55:52 Add a drawing to the worksheet
21:55:52 Add a drawing to the worksheet
21:55:52 Play around with inserting and removing rows and columns
21:55:52 Set header/footer
21:55:52 Set page orientation and size
21:55:52 Rename first worksheet
21:55:52 Create a second Worksheet object
21:55:52 Add some data
21:55:52 Set the worksheet tab color
21:55:52 Set alignments
21:55:52 Set column widths
21:55:52 Set fonts
21:55:52 Add a drawing to the worksheet
21:55:52 Set page orientation and size
21:55:52 Rename second worksheet
21:55:52 Hide grid lines
21:55:52 Set orientation to landscape
21:55:52 Write to PDF format using tcPDF
Notice: Array to string conversion in /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/HTML.php on line 1236 Call Stack: 0.0010 258104 1. {main}() /inet/xxx/HTTP/devel/21pdf.php:0 0.1375 8979304 2. PHPExcel_Writer_PDF->save() /inet/xxx/HTTP/devel/21pdf.php:80 0.1375 8979656 3. PHPExcel_Writer_PDF->__call() /inet/xxx/HTTP/devel/21pdf.php:80 0.1375 8980096 4. call_user_func_array() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/PDF.php:87 0.1375 8980432 5. PHPExcel_Writer_PDF_tcPDF->save() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/PDF.php:87 0.1569 9542416 6. PHPExcel_Writer_HTML->generateSheetData() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/PDF/tcPDF.php:119 0.2014 9683184 7. PHPExcel_Writer_HTML->_generateRow() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/HTML.php:436 Warning: Illegal string offset 'width' in /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/HTML.php on line 1257 Call Stack: 0.0010 258104 1. {main}() /inet/xxx/HTTP/devel/21pdf.php:0 0.1375 8979304 2. PHPExcel_Writer_PDF->save() /inet/xxx/HTTP/devel/21pdf.php:80 0.1375 8979656 3. PHPExcel_Writer_PDF->__call() /inet/xxx/HTTP/devel/21pdf.php:80 0.1375 8980096 4. call_user_func_array() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/PDF.php:87 0.1375 8980432 5. PHPExcel_Writer_PDF_tcPDF->save() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/PDF.php:87 0.1569 9542416 6. PHPExcel_Writer_HTML->generateSheetData() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/PDF/tcPDF.php:119 0.2014 9683184 7. PHPExcel_Writer_HTML->_generateRow() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/HTML.php:436 Warning: Invalid argument supplied for foreach() in /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/HTML.php on line 1315 Call Stack: 0.0010 258104 1. {main}() /inet/xxx/HTTP/devel/21pdf.php:0 0.1375 8979304 2. PHPExcel_Writer_PDF->save() /inet/xxx/HTTP/devel/21pdf.php:80 0.1375 8979656 3. PHPExcel_Writer_PDF->__call() /inet/xxx/HTTP/devel/21pdf.php:80 0.1375 8980096 4. call_user_func_array() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/PDF.php:87 0.1375 8980432 5. PHPExcel_Writer_PDF_tcPDF->save() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/PDF.php:87 0.1569 9542416 6. PHPExcel_Writer_HTML->generateSheetData() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/PDF/tcPDF.php:119 0.2014 9683184 7. PHPExcel_Writer_HTML->_generateRow() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/HTML.php:436 0.2019 9685336 8. PHPExcel_Writer_HTML->_assembleCSS() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/HTML.php:1267 21:55:53 File written to 21pdf_tcPDF.pdf
Call time to write Workbook was 0.9275 seconds
21:55:53 Current memory usage: 16 MB
21:55:53 Peak memory usage: 18 MB
21:55:53 Done writing files
File has been created in /inet/xxx/HTTP/devel
Interesting side note: take a look at the different output of tcpdf and mPDF. I think I know now, why you leave the choice of the PDF generator open to the user.
Quick and Dirty fix;
Change line 1236 of PHPExcel/Writer/HTML.php, which reads:
$cssClass .= ' style' . $pSheet->getCell($endCellCoord)->getXfIndex();
to
if (!$this->_useInlineCss) {
$cssClass .= ' style' . $pSheet->getCell($endCellCoord)->getXfIndex();
}
Related
iam sending pdf file as multipart/form-data using rest api,
and receiving it using(#RequestParam("File") final MultipartFile multipartFile ) in the method.
the received pdf file(multipartFile) pages are inverted, but they have 0 rotation at sending end.
when i use pdfbox to get rotation of this received pdf file page it gives 180,
eventhough when i change rotation to 0 and save as new file, saved file pages are inverted(rotation=180).
public ResponseEntity<?> createDocument(#RequestParam("File") final MultipartFile multipartFile ){
try (InputStream inputStream = new ByteArrayInputStream(multipartFile .getBytes());
PDDocument inputPDFDocument = PDDocument.load(inputStream)) {
inputPDFDocument .getDocumentCatalog().getPages().get(0).getRotation();// received file page rotation =180
inputPDFDocument .getDocumentCatalog().getPages().get(0).setRotation(0);// changed rotation to 0
PDDocument outputPDFDocument = new PDDocument();
outputPDFDocument.addPage(pdfDocument.getDocumentCatalog().getPages().get(0));// adding page to outputpdf
File outputFile=new File(path);// creating a new file
PDDocument.save(outFile);// saving pdf to file, saved file pages are inverted.
}
}
case 1: Test Page : normally opened in pdf/browser
case 1 Test Page corresponding properties
in case 1 test page has no rotation, But in properties rotate=180.
when i send this page to backend (iam using pdfbox in backend) and save the page.
saved page is 180 degree rotated(does not look correct in viewer, the viewed page is 180 degree rotated).
Case 2: Setting rotate=0 in test page properties. this causes the page to be inverted.
case 2 Test Page corresponding properties
when i save this case 2 page using backend, saved page displays no rotation(looks correct in a viewer).
Any help will be greatly appreciated.
thanks
I use CKEDITOR 4, in my ls:
CKEDITOR.replace('ae-textarea-text', {
filebrowserImageUploadUrl: '/Sites/UploadImgToSite?Site=' + $("#SiteName").val(),
height: 850,
width: 800
});
When I upload a images, parametr Site=' + $("#SiteName").val() - is empty, because
1) page open
2) CKEDITOR init with $("#SiteName").val() (now it empty)
3) user fill texbox #SiteName....
That parameter has to be available before CKEditor creation code is executed because it is no longer possible to change that parameter dynamically afterwards. It can only be set once when editor is created.
In your case you could try opening the page, getting siteName from user and only then creating the editor.
I have a PDF with fillable forms. I can successfully fill the forms and save the new PDF to my server no problem, but I cannot place an image on the PDF. When I remove the Image(....) line the script works great. When I add it back it I get the error.
I think it's trying to look for the method in the wrong file, originally I only included fpdm..php but I tried adding back fpdf.php and it did not help. Not sure what I'm doing wrong.
Error:
Fatal error: Call to undefined method FPDM::Image() in /home/.../formPDF.php on line 113
Code:
require('../forms/pdf/fpdf.php');
require('../forms/pdf/fpdm.php');
$pdf = new FPDM($formPDFLocation);
$pdf->Image('images/sig_37-1405313221.png', 100, 20);
$pdf->Load($fields, false); // second parameter: false if field values are in ISO-8859-1, true if UTF-8
$pdf->Merge();
$filename= "../forms/generated/" . $ffID;
$pdf->Output($filename.'.pdf','I');
FPDM is not really related to FPDF but simply a script of a FPDF user. You cannot use FPDF methods on an FPDM instance.
If you search for an all in one solution for such task you may take a look at the SetaPDF-FormFiller component (not free!).
I was able to save the PDF to my server and then add the image but lost the form contents. Code for anyone that is interested in this half solution:
Code for adding image:
<?php
require_once('fpdf.php');
require_once('fpdi.php');
$pdf = new FPDI();
$pdf->AddPage();
$pdf->setSourceFile("16.pdf");
$tplIdx = $pdf->importPage(1);
$pdf->useTemplate($tplIdx, 10, 10, 100);
$pdf->Image('car.jpg',20,100);
$pdf->Output();
?>
Is there a way to use Excel-VBA code in order to make a picture object in a sheet, so as to insert it as a footer image. I have tried to do so by creating a chart object and pasting it in a picture-format, and then exporting the chart to an image file and setting the image as the footer. Is there a better way to insert a picture object as a footer image, and if so, how do I do it?
I started the macro recorder. I clicked Page Setup then Header/Footer then Custom Footer. I clicked the centre section and then Format Picture (button with image of sun over mountains). I browsed for an image and clicked Insert. "&[Picture]" appeared in the centre section. I clicked OK twice. I switched the macro recorder off.
I printed the page and the selected image appeared at the bottom.
The important code saved by the macro recorder was:
ActiveSheet.PageSetup.CenterFooterPicture.Filename = _
"C:\Users\Public\Pictures\Sample Pictures\Desert Landscape.jpg"
Replace "C:\Users\Public\Pictures\Sample Pictures\Desert Landscape.jpg" with filename of your choice.
The macro recorder is usually the easiest way of discovering statements like this.
For anybody viewing this in the future, I'll share my code to copy a range and save it as a file on your computer, which can then be added to the footer. You can eliminate whatever bits you don't want =)
Dim objPic As Shape
Dim objChart As Chart
Dim strTimeStamp As String
Dim strFileDest As String
20 Sheets(2).Activate
30 Sheets(2).Columns("R:T").AutoFit
40 Sheets(2).Rows("17:21").AutoFit
50 ActiveWindow.DisplayGridlines = False
60 Call Sheets(2).Range("S17", "U21").CopyPicture(xlScreen, xlPicture)
70 ActiveWindow.DisplayGridlines = True
80 Sheets(2).Shapes.AddChart
90 Sheets(2).Activate
100 Sheets(2).Shapes.Item(1).Select
110 Set objChart = ActiveChart
120 ActiveChart.Parent.Name = "FooterChart"
' For some reason, Excel occasionally tries to make an actual chart out of these strings.
' It's just a nonsensical chart that messes the footer up but I'm having trouble duplicating the issue and figuring out what causes it.
' This should always work. Don't use .Clear, it crashes.
130 ActiveChart.ChartArea.ClearContents
140 objChart.Paste
150 Selection.Name = "FooterImage"
160 ActiveSheet.ChartObjects("FooterChart").Activate
170 Sheets(2).Shapes.Item(1).Line.Visible = msoFalse
180 Sheets(2).Shapes.Item(1).Height = Range("S17", "U21").Height
190 Sheets(2).Shapes.Item(1).Width = Range("S17", "U21").Width
200 ActiveChart.Shapes.Range(Array("FooterImage")).Height = Range("S17", "U21").Height
210 ActiveChart.Shapes.Range(Array("FooterImage")).Width = Range("S17", "U21").Width
220 Sheets(2).Shapes.Item(1).Height = Sheets(2).Shapes.Item(1).Height * 1.25
230 Sheets(2).Shapes.Item(1).Width = Sheets(2).Shapes.Item(1).Width * 1.25
240 ActiveChart.Shapes.Range(Array("FooterImage")).Height = ActiveChart.Shapes.Range(Array("FooterImage")).Height * 1.2
250 ActiveChart.Shapes.Range(Array("FooterImage")).Width = ActiveChart.Shapes.Range(Array("FooterImage")).Width * 1.2
260 strTimeStamp = CStr(Format(Now(), "yyyymmddHhNnSs"))
270 strFileDest = "D:\Temp" & strTimeStamp & ".jpg"
280 objChart.Export strFileDest
290 InsertPicture strFileDest
300 If Len(Dir$(strFileDest)) > 0 Then
310 Kill strFileDest
320 End If
330 Sheets(2).Shapes.Item(1).Delete
Try this:
Dim ws as Worksheet
Set ws = Worksheets("YourWorksheetName")
With ws.PageSetup
.CenterFooterPicture = "&G" 'Specifies that you want an image in your footer
.CenterFooterPicture.Filename = "C:\Pictures\MyFooterImage.jpg" 'specifies the image file you want to use
End With
The code generated by the macro recorder will get you part of the way there, but as is often the case, it doesn't provide the whole or most appropriate solution. It also sounds like you are trying to insert an image generated by Excel (such as a chart) into the footer? if that's the case, I believe you will have to same the object as an image and then reference that image file.
I have this huge VB project which i just got from some one. i want to add a check box to a dialog in it so i opened Main2.frm and added a check box using ide. but now when i run the application the dialog resized to its orignal size hence not showing the new check box which is at the bottom. I know that this means somewhere in the code there might be something resizing the dialog box but i have scaned the code and found nothing. where the dialog is being created this is being done:
Main2.vsCodeOptions.Left = 10
Main2.vsCodeOptions.Top = 0
Main2.Move main.Left + (main.Width - Main2.Width) \ 2, main.Top + (main.Height - Main2.Height) \ 2, 3900, 5010
Main2.VSImport.Visible = False
Main2.VSAlload.Visible = False
Main2.VsrepPrt.Visible = False
Main2.VSAbout.Visible = False
Main2.vsCodeOptions.Visible = True
Main2.Left = main.Left + (main.Width - Main2.Width) \ 2
Main2.Top = main.Top + (main.Height - Main2.Height) \ 2
'Use this code to make the form stay on top in the form module:
SetWindowPos Main2.hWnd, HWND_TOPMOST, Main2.Left, Main2.Top, 0, 0, SWP_NOMOVE + SWP_NOSIZE
one more thing i am new to VB and VB editor. i want to all controls in a dialog to move collectively, one method is to select them one by one but i want to select all and then move them to make room for new dialog –
thanks
The first line of your code snippet - doesn't it set the size of Main2? To 3900 x 5010? And then a few lines later you move the form again (to exactly where it is) for no good reason.
I'm a little thrown off by all the \ - should they be / ? Did you type this or paste this?
Try to make the position of the control relative to the Top and Left properties of the form. This way it will always be at the same position no matter what the size of the form is.