Afbeelding toevoegen aan pdf

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Marko Westerveld

Marko Westerveld

11/01/2017 12:57:10
Quote Anchor link
Goede middag,

Op de 1 of andere manier doe ik iets fout met het toevoegen van een plaatje in een pdf bestand. Ik probeer het volgende:

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
public function drawImage()
    {
        
        $image = Zend_Pdf_Image::imageWithPath('/home/**/domains/**/public_html/***.png');
        $this->drawImage($image, 10, 10 , 211, 64);
        
    }


Maar ik krijg de volgende foutmelding over de max execution time. En als ik die verleng krijg ik een error dat ik te weinig geheugen heb.
Waarschijnlijk zie ik iets over het hoofd, dat ik iets niet afsluit o.i.d. want een .png van een paar kb moet toch geen problemen geven.

Script:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php
class VWM_Invoice extends VWM_Pdf implements VWM_Templates_Interface
{
    /**
    * Generate PDF
    *
    * @param int $orderCode
    * @param array $orderData
    * @param array $orderItems
    * @param array $orderPaper
    * @return void
    */

    public function generatePdf($orderCode, $orderData, $orderItems, $orderPaper = false)
    {

        // Order data
        $oDebtors = new Debtors();
        $debtorData = $oDebtors -> find($orderData['invoice_debID']) -> current() -> toArray();
        
        $oSettings = new Settings();
        
        $this -> _data['company']['name'] = $oSettings -> get('companyName');
        $this -> _data['company']['website'] = $oSettings -> get('companyWebsite');
        $this -> _data['company']['email'] = $oSettings -> get('companyEmail');
        $this -> _data['company']['address'] = $oSettings -> get('companyAddress');
        $this -> _data['company']['zipcode'] = $oSettings -> get('companyZipcode');
        $this -> _data['company']['place'] = $oSettings -> get('companyPlace');
        $this -> _data['company']['phonenr'] = $oSettings -> get('companyPhonenr');
        $this -> _data['company']['faxnr'] = $oSettings -> get('companyFaxnr');
        $this -> _data['company']['kvknr'] = $oSettings -> get('companyKvknr');
        $this -> _data['company']['vatnr'] = $oSettings -> get('companyVatnr');
        $this -> _data['company']['ibannr'] = $oSettings -> get('companyPaymentIBANnr');
        $this -> _data['company']['bicnr'] = $oSettings -> get('companyPaymentBICnr');
        $this -> _data['company']['billnr'] = $oSettings -> get('companyPaymentBillnr');
        $this -> _data['company']['tnv'] = $oSettings -> get('companyPaymentTNV');
        
        $this -> _data['customer']['code'] = $debtorData['debCode'];
        $this -> _data['customer']['companyName'] = $debtorData['debCompanyname'];
        $this -> _data['customer']['firstName'] = $debtorData['debFirstname'];
        $this -> _data['customer']['lastName'] = $debtorData['debSurname'];
        $this -> _data['customer']['address'] = $debtorData['debAddress'];
        $this -> _data['customer']['zipcode'] = $debtorData['debZipcode'];
        $this -> _data['customer']['place'] = $debtorData['debPlace'];
        $this -> _data['customer']['country'] = $debtorData['debCountry'];
        $this -> _data['customer']['vatnr'] = $debtorData['debVatnr'];
        
        $this -> _data['order']['code'] = $orderCode;
        $this -> _data['order']['date'] = date('d-m-Y', strtotime($orderData['invoiceDate']));
        $this -> _data['order']['priceExcl'] = number_format($orderData['invoicePriceExcl'], 2, ',', '.');
        $this -> _data['order']['priceVat'] = number_format($orderData['invoicePriceVat'], 2, ',', '.');
        $this -> _data['order']['priceIncl'] = number_format($orderData['invoicePriceIncl'], 2, ',', '.');
        $this -> _data['order']['discount'] = ($orderData['invoiceDiscount'] != 0) ? $orderData['invoiceDiscount'] : 0;
        $this -> _data['order']['paymentTerms'] = 'Te betalen binnen ' . $orderData['invoicePayment'] . ' dagen na de factuurdatum op rekeningnummer ' . $this -> _data['company']['billnr'] . ' t.n.v. ' . $this -> _data['company']['tnv'] . ' onder vermelding van klantnummer en factuurnummer.';
        
        //$this -> _data['columns'][] = array('value' => 'Prodnr', 'size' => 40, 'align' => 'right', 'font' => Zend_Pdf_Font::FONT_HELVETICA_BOLD, 'fontSize' => 12);
        $this -> _data['columns'][] = array('value' => 'Aantal', 'size' => 40, 'align' => 'center', 'font' => Zend_Pdf_Font::FONT_HELVETICA_BOLD, 'fontSize' => 12);
        $this -> _data['columns'][] = array('value' => 'Omschrijving', 'margin' => 10, 'font' => Zend_Pdf_Font::FONT_HELVETICA_BOLD, 'fontSize' => 12);
        $this -> _data['columns'][] = array('value' => 'Prijs / stuk', 'size' => 60, 'align' => 'right', 'font' => Zend_Pdf_Font::FONT_HELVETICA_BOLD, 'fontSize' => 12);
        $this -> _data['columns'][] = array('value' => 'Subtotaal', 'size' => 60, 'align' => 'right', 'font' => Zend_Pdf_Font::FONT_HELVETICA_BOLD, 'fontSize' => 12);
        
        $this -> _data['recordsFont'] = array('font' => 'Helvetica', 'fontSize' => 12, 'fontColor' => '#000000');
        foreach($orderItems as $orderItem)
        {

            if(!isset($orderItem['invoiceprodPriceRow']))
            {

                $orderItem['invoiceprodPriceRow'] = $orderItem['invoiceprodCount'] * $orderItem['invoiceprodPriceExcl'];
            }

        
            $this -> _data['records'][] = array(
                //$orderItem['invoiceprod_prodCode'],
                $orderItem['invoiceprodCount'],
                $orderItem['invoiceprodDesc'],
                number_format($orderItem['invoiceprodPriceExcl'], 2, ',', '.'),
                number_format($orderItem['invoiceprodPriceRow'], 2, ',', '.')
            );
        }

        
        // Generate pdf
        $file = APPLICATION_PATH . '/data/templates/template.pdf';
        if(file_exists($file) && $orderPaper === false)
        {

            $this -> setPdf(Zend_Pdf::load($file));
            $this -> setTemplate($this -> getPdf() -> pages[0]);
            $this -> setPage(new Zend_Pdf_Page($this -> getTemplate()));
            $this -> getPdf() -> pages[] = $this -> getPage();
            
            unset($this -> getPdf() -> pages[0]);
        }

        else
        {
            $this -> setPdf(new Zend_Pdf());
            $this -> setPage($this -> getPdf() -> newPage(Zend_Pdf_Page::SIZE_A4));
            $this -> getPdf() -> pages[] = $this -> getPage();
        }

        
        $this -> createPageHeader();
        $this -> createCustomerBox();
        $this -> createOrderDetails();
        $this -> createOrderGrid();
        $this -> createPageFooter();
        //$this -> drawImage();
        
        $this -> getPdf() -> save(APPLICATION_PATH . '/data/invoices/' . $this -> _data['order']['code'] . '.pdf');
    }

    
    /**
    * Create page header
    *
    * @return void
    */

    public function drawImage()
    {

        
        $image = Zend_Pdf_Image::imageWithPath('/home/**/domains/**/public_html/**.png');
        $this->drawImage($image, 10, 10 , 211, 64);
        
    }

    
    
    public function createPageHeader()
    {

        $topPosition = self::HEADER_TOP;
        
        $this -> setFontAndSize('Helvetica', 8);
        
        
        $this -> write($this -> _data['company']['name'], 270, $topPosition);
        $this -> write($this -> _data['company']['address'], 270, $topPosition - 10);
        $this -> write($this -> _data['company']['zipcode'] . ' ' . $this -> _data['company']['place'], 270, $topPosition - 20);
        $this -> write('Email', 270, $topPosition - 30);
        $this -> write($this -> _data['company']['email'], 270, $topPosition - 40);
        
        $this -> write('Telefoon', 390, $topPosition);
        $this -> write($this -> _data['company']['phonenr'], 390, $topPosition - 10);
        $this -> write('Fax', 390, $topPosition - 20);
        $this -> write($this -> _data['company']['faxnr'], 390, $topPosition - 30);
        $this -> write('Web', 390, $topPosition - 40);
        $this -> write($this -> _data['company']['website'], 390, $topPosition - 50);
        
        $this -> write('BTW nr', 490, $topPosition);
        $this -> write($this -> _data['company']['vatnr'], 490, $topPosition - 10);
        $this -> write('KVK nr', 490, $topPosition - 20);
        $this -> write($this -> _data['company']['kvknr'], 490, $topPosition - 30);
        
        $this -> setPageHeight($topPosition - 90);
    }

    
    /**
    * Create customer box
    *
    * @return void
    */

    public function createCustomerBox()
    {

        $topPosition = $this -> getPageHeight();
        
        $this -> setFontAndSize('Helvetica', 12);
        
        if(!empty($this -> _data['customer']['companyName']))
        {

            $this -> write($this -> _data['customer']['companyName'], 300, $topPosition - 15);
        }

        else
        {
            $this -> write($this -> _data['customer']['firstName'] . ' ' . $this -> _data['customer']['lastName'], 300, $topPosition - 15);
        }

        
        $this -> write($this -> _data['customer']['address'], 300, $topPosition - 27);
        $this -> write($this -> _data['customer']['zipcode'] . ' ' . $this -> _data['customer']['place'], 300, $topPosition - 39);
        $this -> write($this -> _data['customer']['country'], 300, $topPosition - 50);
        
        $pageHeight = $this -> getPageHeight();
        $this -> setPageHeight($pageHeight -= 130);
    }

    
    /**
    * Create order details
    *
    * @return void
    */

    public function createOrderDetails()
    {

        $topPosition = $this -> getPageHeight();
    
        $this -> setFontAndSize(Zend_Pdf_Font::FONT_HELVETICA_BOLD, 22);
        $this -> write('Factuur', self::PAGE_BOTH_MARGIN + 10, $topPosition + 40);
        
        $this -> setFontAndSize('Helvetica', 12);
        
        $topPosition += 20;
        $this -> write('Factuurnr', self::PAGE_BOTH_MARGIN + 10, $topPosition);
        $this -> write($this -> _data['order']['code'], self::PAGE_BOTH_MARGIN + 100, $topPosition);
        
        $this -> write('Klantnr', self::PAGE_BOTH_MARGIN + 280, $topPosition);
        $this -> write($this -> _data['customer']['code'], self::PAGE_BOTH_MARGIN + 370, $topPosition);
        
        $topPosition -= 12;
        $this -> write('Factuurdatum', self::PAGE_BOTH_MARGIN + 10, $topPosition);
        $this -> write($this -> _data['order']['date'], self::PAGE_BOTH_MARGIN + 100, $topPosition);
        
        $this -> setPageHeight($topPosition -= 10);
    }

    
    /**
    * Create summary
    *
    * @return void
    */

    public function createSummary()
    {

        $topPosition = $this -> getPageHeight();
    
        $this -> getPage() -> setLineWidth(0.5);
        $this -> getPage() -> setLineColor(new Zend_Pdf_Color_Html('#000000'));
        $this -> getPage() -> drawLine(510, $topPosition + 10, self::PAGE_WIDTH - self::PAGE_BOTH_MARGIN, $topPosition + 10);
        
        $this -> setFontAndSize('Helvetica', 12);
        
        $this -> writeText('Subtotaal €', 450, $topPosition, 50, 'right', Zend_Pdf_Font::FONT_HELVETICA, 12);
        $this -> writeText($this -> _data['order']['priceExcl'], 565, $topPosition, 0, 'right', Zend_Pdf_Font::FONT_HELVETICA, 12);
        $topPosition -= 15;
        
        $this -> writeText('BTW €', 450, $topPosition, 50, 'right', Zend_Pdf_Font::FONT_HELVETICA, 12);
        $this -> writeText($this -> _data['order']['priceVat'], 565, $topPosition, 0, 'right', Zend_Pdf_Font::FONT_HELVETICA, 12);
        $topPosition -= 15;
        
        if($this -> _data['order']['discount'] != 0)
        {

            $this -> setFontAndSize('Helvetica', 12);
            $this -> writeText('Korting %', 450, $topPosition, 50, 'right', Zend_Pdf_Font::FONT_HELVETICA, 12);
            $this -> writeText($this -> _data['order']['discount'], 565, $topPosition, 0, 'right', Zend_Pdf_Font::FONT_HELVETICA, 12);
            $topPosition -= 15;
        }

        
        $this -> getPage() -> drawLine(510, $topPosition + 10, self::PAGE_WIDTH - self::PAGE_BOTH_MARGIN, $topPosition + 10);
        $this -> setFontAndSize('Helvetica', 12);
        $this -> writeText('Totaal €', 450, $topPosition, 50, 'right', Zend_Pdf_Font::FONT_HELVETICA_BOLD, 12);
        $this -> writeText($this -> _data['order']['priceIncl'], 565, $topPosition, 0, 'right', Zend_Pdf_Font::FONT_HELVETICA_BOLD, 12);
        
        $pageHeight = $this -> getPageHeight();
        $this -> setPageHeight($pageHeight -= 100);
    }

    
    /**
    * Create page footer
    *
    * @return void
    */

    public function createPageFooter()
    {

        $topPosition = $this -> getPageHeight() - 10;
        
        $this -> setFontAndSize('Helvetica', 12);
        
        $this -> writeText($this -> _data['order']['paymentTerms'], self::PAGE_BOTH_MARGIN * 2, $topPosition, (self::PAGE_WIDTH - (self::PAGE_BOTH_MARGIN * 2)) - 45, 'center', Zend_Pdf_Font::FONT_HELVETICA, 10);
    }

    
    /**
    * Create page
    *
    * @return void
    */

    public function createNewPage()
    {

        $template = $this -> getTemplate();
        if(!empty($template))
        {

            $this -> setPage(new Zend_Pdf_Page($template));
        }

        else
        {
            $this -> setPage($this -> getPdf() -> newPage(Zend_Pdf_Page::SIZE_A4));
        }

        
        $this -> getPdf() -> pages[] = $this -> getPage();
        
        $this -> createPageHeader();
    }

    
    /**
    * Download PDF
    *
    * @param int $invoiceID
    * @return  void | bool
    */

    public function downloadPdf($invoiceID)
    {

        $oInvoices = new Invoices();
        $invoiceData = $oInvoices -> find($invoiceID) -> current();
        
        if($invoiceData)
        {

            $invoiceData -> toArray();
            
            if($invoiceData['invoice_debID'] == VWM_Auth::getIdentity() -> user_debID)
            {

                if($invoiceData['invoiceStatus'] != 1)
                {

                    header('Content-type:application/x-pdf');
                    header('Content-Disposition: attachment; filename=factuur_' . $invoiceData['invoiceCode'] . '.pdf');
                    readfile(APPLICATION_PATH . '/data/invoices/' . $invoiceData['invoiceCode'] . '.pdf');
                    exit;
                }

                
                return false;
            }

            elseif(VWM_Auth::getIdentity() -> userRole == VWM_Acl::ADMIN)
            {

                header('Content-type:application/x-pdf');
                header('Content-Disposition: attachment; filename=factuur_' . $invoiceData['invoiceCode'] . '.pdf');
                readfile(APPLICATION_PATH . '/data/invoices/' . $invoiceData['invoiceCode'] . '.pdf');
                exit;
            }

            
            return false;
        }

        
        return false;
    }
}

?>

- Ariën -:
Gelieve in het vervolg bij code de [code][/code]-tags gebruiken.
Hier kan je meer lezen over de mogelijke opmaakcodes.
Alvast bedankt!
Gewijzigd op 11/01/2017 12:59:34 door - Ariën -
 
PHP hulp

PHP hulp

19/04/2024 23:48:53
 
Marlies Maalderink

Marlies Maalderink

11/01/2017 13:41:52
Quote Anchor link
Misschien heb ik het mis maar heb je hier niet gewoon te maken met een infinite loop?

Je hebt een functie drawImage die in zichzelf ook weer drawImage aanroept.
 
Eddy E

Eddy E

11/01/2017 21:25:07
Quote Anchor link
Wat direct verklaart waarom het 30 seconden moet duren (hij blijft immers bezig).
En zodra je hem 'bezig' laat zijn tot in eeuwigheid (zeg, 90 seconden), kan hij toch dat plaatje 900.000 keer toevoegen.... en dan zit je geheugen op je shared-hosting toch best snel vol.
 



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.