2

I have an html page like JsFiddle and I want convert this in pdf, i can't create the line to line pdf because the page is dinamically create, I use php for calling a fiel that connect to mysql and fill a template file like.

$_POST['IdQuestionario']=5;
$_POST['IdUtente']=10001;
$_POST['Visualizza']=true;
$_POST['IdImpianto']=1;
$_POST['Stampa']=true;
$_POST['TipoImpianto']='grande';

ob_start();
ob_clean();
require_once 'intro.php';
$tbl=ob_get_clean();
$html.=$tbl;

I'm trying with tcpf, mpdf , jsPDF but i cant obtain a discrete output because I use colgroup for table. anyone say me a method for render the page,if is possible whitout install software on server.

Ossarotte
  • 325
  • 4
  • 12
  • did you try http://www.fpdf.org ? you can insert html as well – George G Jul 11 '16 at 13:30
  • I try but the file is not well format – Ossarotte Jul 12 '16 at 06:33
  • can you post full code that you use? – George G Jul 12 '16 at 06:35
  • Simple I have many template file and for create the page that view in jsfiddle I call a mysql db and fill the teplate via import like `$_POST['IdQuestionario']=5; $_POST['IdUtente']=10001; $_POST['Visualizza']=true; $_POST['IdImpianto']=1; $_POST['Stampa']=true; $_POST['TipoImpianto']='grande'; ob_start(); ob_clean(); require_once 'intro.php'; $tbl=ob_get_clean(); $html.=$tbl;` – Ossarotte Jul 12 '16 at 09:22

3 Answers3

2

There a few that i know of - some have problems with tables, I would avoid DOMPDF - known issues with tables.

There's one that's recommended from cvision; i don't have a code sample, but you can download it free and even sample it online.

There's also a php-pdf product available from muhimbi (a little lesser-known!, but i think it's free)

<?php
// Include the generated proxy classes
require_once "documentConverterServices.php";
// Check the uploaded file
if ($_FILES["file"]["error"] > 0)
{
    echo "Error uploading file: " . $_FILES["file"]["error"];
}
else 
{
    // Get the uploaded file content
    $sourceFile = file_get_contents($_FILES["file"]["tmp_name"]);

    // Create OpenOptions
    $openOptions = new OpenOptions();
    // set file name and extension
    $openOptions->FileExtension = pathinfo($_FILES["file"]["name"], PATHINFO_EXTENSION);
    $openOptions->OriginalFileName = $_FILES["file"]["name"];
    // Create conversionSettings
    $conversionSettings = new ConversionSettings();
    // Set the output format
    if(isset($_POST["outputFormat"]))
    {
        $conversionSettings->Format = $_POST["outputFormat"];
    } else {
        $conversionSettings->Format = "PDF";
    }
    // Set fidelity
    $conversionSettings->Fidelity = "Full";
    // These values must be set to empty strings or actual passwords when converting to non PDF formats
    $conversionSettings->OpenPassword="";
    $conversionSettings->OwnerPassword="";
    // Set some of the other conversion settings. Completely optional and just an example
    $conversionSettings->StartPage = 0;
    $conversionSettings->EndPage = 0;
    $conversionSettings->Range = "VisibleDocuments";
    $conversionSettings->Quality = "OptimizeForPrint";
    $conversionSettings->PDFProfile = "PDF_1_5";
    $conversionSettings->GenerateBookmarks = "Automatic";
    $conversionSettings->PageOrientation="Default";
    // Create the Convert parameter that is send to the server
    $convert = new Convert($sourceFile, $openOptions, $conversionSettings);
    // Create the service client and point it to the correct Conversion Service
    $url = "http://localhost:41734/Muhimbi.DocumentConverter.WebService/?wsdl";
    $serviceClient = new DocumentConverterService(array(), $url);

    // If you are expecting long running operations then consider longer timeouts
    ini_set('default_socket_timeout', 60);

    try 
    {
        // Execute the web service call
        $result = $serviceClient->Convert($convert)->ConvertResult;
        // Send the resulting file to the client.
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Content-type: application/octet-stream");
        header("Content-Disposition: attachment; filename=\"convert." . $conversionSettings->Format . "\"");
        echo $result;
    }
    catch (Exception $e) 
    {
        print "Error converting document: ".$e->getMessage();
    }    
}
?>

Also, you could investigate 'Snappy' (has dependencies)

Rachel Gallen
  • 27,943
  • 21
  • 72
  • 81
  • https://tcpdf.org/ - currently not free, but soon to be free (new version under development on github. http://docraptor.com/ - also paid, but free trial. – Rachel Gallen Jul 11 '16 at 18:22
2

You can try WKHTMLTOPDF.

Here is a Stackoverflow Thread on how to use it with PHP.

How do I get WKHTMLTOPDF to execute via PHP?

And here is a wrapper for PHP https://github.com/mikehaertl/phpwkhtmltopdf

Community
  • 1
  • 1
Salvador P.
  • 1,469
  • 19
  • 16
1

MPDF one of best library to convert pdf, try it

MPDF link : http://www.mpdf1.com/mpdf/index.php

Example link : http://mpdf1.com/common/mpdf/examples/

Hiren Makwana
  • 1,976
  • 2
  • 13
  • 28