Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7716697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:41:53+00:00 2026-06-01T02:41:53+00:00

I have a quite complicated HTML/CSS layout which I would like to convert to

  • 0

I have a quite complicated HTML/CSS layout which I would like to convert to PDF on my server. I already have tryed DOMPDF, unfortunately it did not convert the HTML with correct layout. I have considered HTMLDOC but I have heard that it ignores CSS to a large extent, so I suppose the layout would break apart with that tool too.

My question therefor is – are there any online demos for other tools (like wkhtmltopdf i.e.) that I could use to verify how my HTML is converted? Before spending the rest of my life installing & testing one by one?

Unfortunately, I can’t change the HTML layout to fit those tools. Or better said – I could, if any of them would get close to an acceptable result…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T02:41:54+00:00Added an answer on June 1, 2026 at 2:41 am

    Not really an answer but for the question above, but I’ll try to provide some of my experience, maybe it will help someone somwhere in the future.

    1. wkthmltopdf is really THE ONLY solution that worked for me that could produce what I call acceptable results. Still, some minor modifications to the CSS had to be made, however, it worked really well when it comes to rendering the content. All the other packages are really only suitable if you have a rather simply document with one basic table etc. No chance to get them to produce fair results on complex docs with design elements, css, multiple overlapping images etc. If complex documents are in game – do not spend the time (like I did) – go straight to wkhtmltopdf.

    2. Beware – the wkhtmltopdf installation is tricky. It was not so easy for me as the guys said in their comments (one of the reasons might be that I am not too familiar with Linux). The static binary did not work for me for some reason I can’t explain. I suspect that there were problems with the version – apparently there is a difference between versions for different OS and processors, maybe I have the vrong version. For installing the non-static version first of all you have to have root access to the server, that’s obvious. I installed it with apt-get using PuTTy, went quite well. I was lucky that my server already had all the predispositions to install wkhtmltopdf. So this was the easy part for me 🙂 (btw, you don’t have to care for symbolic links or wrappers as many tutorials tell you – I spent hours trying to figure out how to do that part, in the end I gave it up and everything works well though)

    3. After the install I got the quite famous Cannot connect to X server error. This is due to the fact that we need to run wkhtmltopdf headless on a ‘virtual’ x server. Getting around this was also quite simple (if one does not care for the symbolic links). I installed it with apt-get install xvfb. This also went quite well for me, no problems.

    4. After completing this I was able to run wkhtmltopdf. Beware – it took me some time to figure out that trying to run xvfb was the wrong way – instead you have to run xvfb-run. My PHP code now looks like this exec("xvfb-run wkhtmltopdf --margin-left 16 /data/web/example.com/source.html /data/web/example.com/target.pdf"); (notice the --margin-left 16 command line option for wkhtmltopdf – it makes my content more centered; I left it in place to demonstrate how you can use command line options).

    5. I also wanted to protect the generated PDF files from editing (in my case, print protect is also possible). After doing some research I found this class from ID Security Suite. First of all I have to say – IT’S OLD (I am running PHP 5+). However, I made some improvements to it. First of all – it’s a wrapper around the FPDF library, so there is a file called fpdf.php in the package. I replaced this file from the latest FPDF version I got from here. It made my PHP warnings look more sustainable. I also changed the $pdf =& new FPDI_Protection(); and removed the & sign as I was getting an deprecated warning for it. However, there are more of those to come. Instead of searching and modifying the code I just turned the error reporting lvl to 0 with error_reporting(0); (although turning off the warnings only should be sufficient). Now someone will say that this is not "good practice". I am using this whole stuff on an internal system, so I do not really have to care. For sure the scripts could be modifiyed to match latest requirements. For me I didn’t want to spend another hours working on it. Be careful where the script says $pdf->SetProtection(array('print'), '', $password); (I allowed printing my documents as you can see). It took me a while to figure out that the first argument is the permissions. The second is the USER PASSWORD – if you provide this then the docs will require a password to open (I left this blank). The third is the OWNER PASSWORD – this is what you need to make the docs "secured" against editing, copying etc.

    My whole code now looks like:

    // get the HTML content of the file we want to convert
    $invoice = file_get_contents("http://www.example.com/index.php?s=invoices-print&invoice_no=".$_GET['invoice_no'];
    // replace the CSS style from a print version to a specially modified PDF version
    $invoice = str_replace('href="design/css/base.print.css"','href="design/css/base.pdf.css"',$invoice);
    
    // write the modified file to disk
    file_put_contents("docs/invoices/tmp/".$_GET['invoice_no'].".html", $invoice);
    
    // do the PDF magic
    exec("xvfb-run wkhtmltopdf --margin-left 16 /data/web/domain.com/web/docs/invoices/tmp/".$_GET['invoice_no'].".html /data/web/domain.com/web/docs/invoices/".$_GET['invoice_no'].".pdf");
    
    // delete the temporary HTML data - we do not need that anymore since our PDF is created
    unlink("docs/invoices/tmp/".$_GET['invoice_no'].".html");
    
    // workaround the warnings
    error_reporting(0); 
    
    // script from ID Security Suite
    function pdfEncrypt ($origFile, $password, $destFile){
        require_once('libraries/fpdf/FPDI_Protection.php');
        $pdf = new FPDI_Protection();
        $pdf->FPDF('P', 'in');
        //Calculate the number of pages from the original document.
        $pagecount = $pdf->setSourceFile($origFile);
        //Copy all pages from the old unprotected pdf in the new one.
        for ($loop = 1; $loop <= $pagecount; $loop++) {
            $tplidx = $pdf->importPage($loop);
            $pdf->addPage();
            $pdf->useTemplate($tplidx);
        }
    
        //Protect the new pdf file, and allow no printing, copy, etc. and
        //leave only reading allowed.
        $pdf->SetProtection(array('print'), '', $password);
        $pdf->Output($destFile, 'F');
        return $destFile;
    }
    
    //Password for the PDF file (I suggest using the email adress of the purchaser).
    $password = md5(date("Ymd")).md5(date("Ymd"));
    //Name of the original file (unprotected).
    $origFile = "docs/invoices/".$_GET['invoice_no'].".pdf";
    //Name of the destination file (password protected and printing rights removed).
    $destFile = "docs/invoices/".$_GET['invoice_no'].".pdf";
    //Encrypt the book and create the protected file.
    pdfEncrypt($origFile, $password, $destFile );
    

    Hope this helps someone to save some time in the future. This whole solution took me like 12 hours to implement into our invoicing system. If there was better info on wkhtmltopdf for users like me, who are not that familiar with Linux/UNIX, I could have saved some of the hours spent on this.

    However – what doesn’t kill you makes you stronger 🙂 So I am a bit more perfect now that I made this run 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have quite complicated query from which I would like to create a view.
I have a quite complicated query which will by built up dynamically and is
I have quite a few RewriteRules in my .htaccess that looks like this RewriteRule
I have quite a lot of PHP view files, which I used to include
I have quite big document in html format that generated from Microsoft Word. It
I have a legacy code implemented in C (not C++). I would like to
I have a HTML form which when submitted sends an email with all the
I have to mock quite complicated java web service and I'm searching for the
I am in the middle of developing a Django application, which has quite complicated
This is a more general question: I have a quite complicated files table in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.