Jump to content

PHP to PDF generating taking long time


Senthilkumar

Recommended Posts

Hi experts,

I am generating PDF report from my php file using DOMPDF library. The code is generating PDF report. But it is taking 60 seconds to generate pdf.

In this PDF i am inserting Logo's & QR Code image.  

If i genereate without any image files, the PDF is generating within 11 sec. But if  i am using logo and QR Code image the PDF is taking more than 60sec's. During generating pdf, the other pages are not loading. 

I am attaching my code here for yur reference purpose. 

Please help me to reduce the PDF generating time.

pdfsoftcopy.txt

Link to comment
Share on other sites

you would need to post an sql dump with some sample data and the logo file to get specific help.

a likely performance problem is using .png for the QR image and putting that image in several places in the document. if you convert it to a jpeg image, it should render more quickly as the jpeg format is natively supported by dompdf. to produce a jpeg for the QR image, see this link - https://phpqrcode.sourceforge.net/examples/index.php?example=711

Link to comment
Share on other sites

Dear mac_gyver,

Thanks for your reply. I changed the QR Generator from png to jpeg format as per the instruction given on the link you provided. But still it is taking long time to generate pdf. Please find hte attached modified code file.

My logo is is attached here

My SQL dump size is 33mb. So not ale to attach here. 

 

Please look in to this and provide solution to generate the pdf fast

CompanyLogo.jpg

TUV Logo.jpg

pdfsoftcopy.txt

Link to comment
Share on other sites

Posting hundreds of lines of your code is NOT the way to get help.  Find the problem area and do your own debugging and then that doesn't help then show us just that portion.  And Stop using Spreadsheets when you are thinking of doing web-development.  They are extremely incompatible.

When Barand asked to 'see your tables' I do believe he meant to 'show us the structure' not the contents.  

AND - when posting stuff POST IT.  Not a link to some text file somewhere.  A lot of people here will not even open a file here for fear of what they might be getting into.  Pick out the code that needs attention and post that.

Link to comment
Share on other sites

6 minutes ago, ginerjm said:

When Barand asked to 'see your tables' I do believe he meant to 'show us the structure' not the contents.

No, I wanted to see the contents too so I could see how the data was being stored (significant, as the example below shows), hence the request for a dump and not structure.

mysql> SELECT * FROM calibrationdata LIMIT 1\G
*************************** 1. row ***************************
                   id: 5800
               Branch: Hyderabad
             Custname: XXXXXX Enterprises Limited
          Siteaddress: XXX XXXXXX, Survey no 296/7/9 , Hyderabad, Telangana
        Officeaddress: XXX XXXXXX, Survey no 296/7/9 , Hyderabad, Telangana
                Model: M1C
             PlantSno: 377
         Inchargename: Mr. Jaleel XXXXXX
             Mobileno: XXXXXXX665
              Emailid: xxxxxx@xxxxxxxxxxxxxxxxx.com
                 Date: 2023-09-25
            SEnggname: xxxxxx xxxxxx xxxxxx
               pmname: Mr. Jaleel xxxxxx
              pmdesig: Incharge
            pmcompany: XXXXXX Enterprises ltd
               QCname: XXXXXX
              QCdesig: Incharge
            QCcompany: XXXXXX Enterprises ltd
          client1name:
         client1desig:
       client1company:
          client2name:
         client2desig:
       client2company:
               Aggmax: 2000
               Cemmax: 600
             Watermax: 300
            CemWtrmax:
            Admix1max: 6000
            Admix2max:
            Silicamax:
               Icemax:
               Callno: SE230925361715
             Calltype: non - warranty
               status: 1
   aggregateascending: {"val_0":"0,0,0","val_1":"100,101,1","val_2":"200,202,2","val_3":"300,301,1","val_4":"400,401,1","val_5":"500,501,1","val_6":"600,602,2","val_7":"700,701,1","val_8":"800,802,2","val_9":"900,903,3","val_10":"1000,1001,1","val_11":"1100,1101,1","val_12":"1200,1201,1","val_13":"1300,1302,2","val_14":"1400,1402,2","val_15":"1500,1502,2","val_16":"1600,1602,2","val_17":"1700,1702,2","val_18":"1800,1802,2","val_19":"1900,1902,2","val_20":"2000,2002,2"}
  aggregatedescending: {"val_0":"2000,2002,2","val_1":"1900,1902,2","val_2":"1800,1802,2","val_3":"1700,1702,2","val_4":"1600,1602,2","val_5":"1500,1502,2","val_6":"1400,1402,2","val_7":"1300,1302,2","val_8":"1200,1202,2","val_9":"1100,1102,2","val_10":"1000,1001,1","val_11":"900,901,1","val_12":"800,801,1","val_13":"700,701,1","val_14":"600,601,1","val_15":"500,501,1","val_16":"400,401,1","val_17":"300,301,1","val_18":"200,201,1","val_19":"100,101,1","val_20":"0,1,1"}
      cementascending: {"val_0":"0,0,0","val_1":"20,19,-1","val_2":"40,39,-1","val_3":"60,59,-1","val_4":"80,80,0","val_5":"100,99,-1","val_6":"120,119,-1","val_7":"140,139,-1","val_8":"160,159,-1","val_9":"180,179,-1","val_10":"200,199,-1","val_11":"220,221,1","val_12":"240,241,1","val_13":"260,261,1","val_14":"280,281,1","val_15":"300,301,1","val_16":"320,321,1","val_17":"340,341,1","val_18":"360,361,1","val_19":"380,381,1","val_20":"400,401,1","val_21":"420,421,1","val_22":"440,441,1","val_23":"460,461,1","val_24":"480,481,1","val_25":"500,501,1","val_26":"520,521,1","val_27":"540,541,1","val_28":"560,561,1","val_29":"580,581,1","val_30":"600,601,1"}
     cementdescending: {"val_0":"600,601,1","val_1":"580,581,1","val_2":"560,561,1","val_3":"540,541,1","val_4":"520,521,1","val_5":"500,501,1","val_6":"480,481,1","val_7":"460,461,1","val_8":"440,441,1","val_9":"420,421,1","val_10":"400,401,1","val_11":"380,381,1","val_12":"360,361,1","val_13":"340,341,1","val_14":"320,321,1","val_15":"300,301,1","val_16":"280,281,1","val_17":"260,259,-1","val_18":"240,239,-1","val_19":"220,219,-1","val_20":"200,199,-1","val_21":"180,181,1","val_22":"160,161,1","val_23":"140,141,1","val_24":"120,121,1","val_25":"100,101,1","val_26":"80,81,1","val_27":"60,61,1","val_28":"40,41,1","val_29":"20,21,1","val_30":"0,1,1"}
       waterascending: {"val_0":"0,0,0","val_1":"20,20,0","val_2":"40,40,0","val_3":"60,60,0","val_4":"80,80,0","val_5":"100,100,0","val_6":"120,120,0","val_7":"140,140,0","val_8":"160,160,0","val_9":"180,180,0","val_10":"200,200,0","val_11":"220,220,0","val_12":"240,240,0","val_13":"260,260,0","val_14":"280,280,0","val_15":"300,300,0"}
      waterdescending: {"val_0":"300,300,0","val_1":"280,280,0","val_2":"260,260,0","val_3":"240,240,0","val_4":"220,220,0","val_5":"200,200,0","val_6":"180,180,0","val_7":"160,160,0","val_8":"140,140,0","val_9":"120,120,0","val_10":"100,100,0","val_11":"80,80,0","val_12":"60,60,0","val_13":"40,40,0","val_14":"20,20,0","val_15":"0,0,0"}
 cementwaterascending: {}
cementwaterdescending: {}
   admixtureascending: {"val_0":"0,0,0","val_1":"1000,1002,2","val_2":"2000,2003,3","val_3":"3000,3002,2","val_4":"4000,4002,2","val_5":"5000,5003,3","val_6":"6000,6002,2"}
  admixturedescending: {"val_0":"6000,6003,3","val_1":"5000,5002,2","val_2":"4000,4002,2","val_3":"3000,3003,3","val_4":"2000,2002,2","val_5":"1000,1002,2","val_6":"0,2,2"}
  admixture2ascending: {}
 admixture2descending: {}
      silicaascending: {}
     silicadescending: {}
         iceascending: {}
        icedescending: {}
            invoiceno: 86431240
               amount: 9440
               enable:
                First: 0000-00-00
               Second: 0000-00-00
                Third: 0000-00-00
          client3name:
         client3desig:
       client3company:
                today: 2023-09-25
            Tolerance: 0

 

Link to comment
Share on other sites

while these points probably have nothing to do with the performance problem (with the exception of the invalidate markup), the code needs to be refactored, corrected, simplified, and cleaned up -

  1. this code is filled with unnecessary copying of variables to other variables and repetitive code that only differs in a variable name/title. just use the original variables that data is in and use a template/function/data-driven design instead of writing out code for every value.
  2. you want to always report all php errors. when learning, developing, and debugging, you want to display all php errors. when on a live/public server, you want to log all php errors.
  3. the error related settings should be in the php.ini on your system, so that you can set or change them at a single place.
  4. use 'require' for things your code must have for it to work and be consistent in what you use.
  5. include/require are not functions. the () around the file name do nothing. leave them out.
  6. the session variable with the user id should either be set and contain the id or it should not be set. don't bother testing if it is an empty string. it should never be one.
  7. many things have have ids. the session variable holding the user id should be uniquely named, something like user_id.
  8. there's no good reason to destroy the whole session. a session can contain more than just the logged in user's id.
  9. all inputs need to be trimmed, then validated before being used. if 'required' input(s) are not valid, don't run the code that's dependent upon them.
  10. every redirect needs an exit/die statement to stop php code execution. currently, all the code on the page runs, even if there is no logged in user.
  11. don't use or die(...) for error handling. use exceptions for database statement error handling (which is the default setting now in php8+) and in most cases simply let php catch and handle any database exception.
  12. mysqli_error() requires the connection variable as a parameter. this problem will go away when you switch to using exceptions for database errors.
  13. use a prepared query when supplying external, unknown, dynamic values to a sql query when it gets executed.
  14. if it seems like using the mysqli extension is overly complicated and inconsistent, especially when dealing with prepared queries, it is. this would be a good time to stitch to the much simpler and more modern PDO extension.
  15. while there should be a user matching the logged in user id, if there isn't, all the code dependent on there being a user should not be executed.
  16. a true value returned by mysqli_query() only indicates that the query executed without error. it doesn't mean that the query matched a row of data. you should instead test that the fetch statement returned a true value. also, all the rest of the code is dependent on there being fetched data. if there isn't, none of the remaining code should be executed.
  17. when mapping an input value to an output value, don't write out conditional logic for every value. use a 'mapping' array instead.
  18. there are a number of html markup errors. you should validate the resulting html at validator.w3.org
  19. don't repeat yourself (DRY) don't repeat conditional tests, just put everything in one conditional test.
  20. multiple spaces in html markup render as a single space. if you do have a need for multiple spaces, you would use a  
  21. instead of escaping double-quotes in the php produced markup, use single-quotes.

as to the performance problem, you need to profile the execution of the code, i.e. measure the time it takes for different sections of code to execute. i would start by timing the execution of the code from the start up to the QR code, the QR code, the generation of the html markup, and each of the main dompdf statements. once you know where the greatest amount of time is being taken, you can concentrate on finding and fixing the performance problems in that section of code.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.