ccc

Mostrando entradas con la etiqueta PDF. Mostrar todas las entradas
Mostrando entradas con la etiqueta PDF. Mostrar todas las entradas

PDF: herramienta online para editar PDFs

 https://www.ilovepdf.com/es


Para poder marcar las casillas de un PDF en ilovepdf tienes que ponerlo en modo Edit, marcas las casillas y luego lo vuelves a poner en modo Anotate para descargarlo o rellenar textos.

PDF: Crear PDFs a partir de HTML mediante PHP

Descargar la librería dompdf-master:

https://github.com/dompdf/dompdf

generarPDF:
require_once 'dompdf-master/lib/html5lib/Parser.php';
require_once 'dompdf-master/src/Autoloader.php';

Dompdf\Autoloader::register();

use Dompdf\Dompdf;

$html = file_get_contents("invoice_header.html");
$table_head = true;

for($i=1;$i<100;$i++) {
    if($table_head) {
        $html .= file_get_contents("invoice_table_head.html"); // la cabecera de las columnas para cada pag
        $table_head = false;
    }

    $html .= "
        <tr>
            <td class='service'>aaa ".$i."</td>
            <td class='desc'>bbb ".$i."</td>
            <td class='unit'>ccc ".$i."</td>
        </tr>";
}

$html .= "
        <tr>
            <td style='background: #9c6c38;' class='service'>Total: ".$i." Registros</td>
            <td style='background: #9c6c38;' class='desc'>-----</td>
            <td style='background: #9c6c38;' class='unit'>----</td>
        </tr>";

$dompdf = new Dompdf();
$dompdf->loadHtml($html);

$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$dompdf->stream();


invoice_header.html:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="style.css" media="all" />
</head>
<body>
<header class="clearfix">
    <div id="logo">
        <img src="milogo-logo.png">
    </div>
    <h1>FACTURA</h1>

    <div id="project">
        <div><span>PROJECT</span> Website development</div>
        <div><span>CLIENT</span> John Doe</div>
        <div><span>ADDRESS</span> 796 Silver Harbour, TX 79273, US</div>
        <div><span>EMAIL</span> <a href="mailto:john@example.com">john@example.com</a></div>
        <div><span>DATE</span> August 17, 2015</div>
        <div><span>DUE DATE</span> September 17, 2015</div>
    </div>
</header>
<main>


invoice_table_head.html: <table>
    <thead>
    <tr>
        <th class="service">SERVICIOS</th>
        <th class="desc">DESCRIPCIÓN</th>
        <th>PRECIO</th>
    </tr>
    </thead>
    <tbody>


Si se quiere añadir imágenes sería:
require_once 'dompdf-master/lib/html5lib/Parser.php';
require_once 'dompdf-master/src/Autoloader.php';

Dompdf\Autoloader::register();
use Dompdf\Dompdf;
use Dompdf\Options;

$dompdf = new Dompdf();
$options = new Options();

$options->set('isRemoteEnabled', TRUE);
$dompdf = new Dompdf($options);
$html = file_get_contents("../prueba.html");

$dompdf->loadHtml($html);

$dompdf->setPaper('A4', '');
$dompdf->render();
$dompdf->stream();


Si se quiere cargar un fichero php no funciona con file_get_contents (pues eso te trae todo el código fuente y no lo ejecuta), la solución es:
function getRemoteFile($url, $timeout = 10) {
  $ch = curl_init();
  curl_setopt ($ch, CURLOPT_URL, $url);
  curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  $file_contents = curl_exec($ch);
  curl_close($ch);
  return ($file_contents) ? $file_contents : FALSE;
}

// hay que poner toda la ruta, no funciona solo poniendo pagina_html3.php
$html = getRemoteFile("http://localhost/pruebas/pdf/htmltopdf/pagina_html3.php");

pdfcrowd: convertir html a PDF

Descargar la librería en:

https://pdfcrowd.com/doc/api/client-library/php/

Convertir una url en un PDF:
require 'pdfcrowd.php';
try
{ 
    // create an API client instance
    $client = new Pdfcrowd("xxx", "xxxx");

    // convert a web page and store the generated PDF into a $pdf variable
    $pdf = $client->convertURI('http://www.midominio.com/');

    // set HTTP response headers
    header("Content-Type: application/pdf");
    header("Cache-Control: max-age=0");
    header("Accept-Ranges: none");
    header("Content-Disposition: attachment; filename=\"result.pdf\"");

    // send the generated PDF
    echo $pdf;
}
catch(PdfcrowdException $why)
{
    echo "Pdfcrowd Error: " . $why;
}


Convertir tu propio código HTML en PDF:
require 'pdfcrowd.php';
try
{
    // create an API client instance
    $client = new Pdfcrowd("xxx", "xxxx");
    //$client->setPageLayout(2);
    $client->setPageHeight(-1); // para q no ponga el num pag

    // convert a web page and store the generated PDF into a $pdf variable
    //$pdf = $client->convertURI('http://www.tspplus.com/test/header.php');
    //$client->setHeaderUrl('http://www.tspplus.com/test/header.php');

    // set HTTP response headers
    header("Content-Type: application/pdf");
    header("Cache-Control: max-age=0");
    header("Accept-Ranges: none");
    header("Content-Disposition: attachment; filename=\"result.pdf\"");

    $pdf_content = "";
    for ($i=1;$i<100;$i++)   {
        $pdf_content .= "<p style='border: solid 1px black;'>Linea de relleno ".$i."</p>";
    }
    $pdf = $client->convertHtml("<head></head><body>".$pdf_content."</body>");
    // send the generated PDF

    echo $pdf;
}
catch(PdfcrowdException $why)
{
    echo "Pdfcrowd Error: " . $why;
}

PHP: Crear PDF a partir de un HTML (incluyendo links, fotos, etc)

Descargar html2pdf de https://github.com/iafan/html2pdf y guardarlo todo en una carpeta llamada html2pdf

Creamos un fichero crear_pdf.php:

ob_start();
include('pagina.php');
$content = ob_get_clean();

// Incluir html2pdf.class.php (esa librería ya luego llama a las carpetas de
// _class, _tcpdf, bin y locale

require_once('html2pdf/html2pdf.class.php');
try
{
$html2pdf = new HTML2PDF('P', 'A4', 'fr', true, 'UTF-8', array(15, 5, 15, 5));
$html2pdf->pdf->SetDisplayMode('fullpage');
$html2pdf->writeHTML($content, isset($_GET['vuehtml']));
$html2pdf->Output('miPDF.pdf');
// Si queremos que en vez de mostrarse se guarde en el disco:
$html2pdf->Output('miPDF.pdf', "F");
}
catch(HTML2PDF_exception $e) {
echo $e;
exit;
}

pagina.php:
<page style="font-size: 10pt">
<h1>Hola mundo</h1>
<p><a href="http://www.google.com" target="_blank">Esto es un enlace</a></p>
<p>Y aqui pondré una imagen:</p>
<img src="http://www.midominio.com/images/imgOscars/ingrid_bergman.jpg">
<?php
for ($i=1;$i<5;$i++) { ?>
<p><?=$i;?></p>
<?php
}
?>
</page>

Crear documentos PDF con PHP

Descargar la librería fpdf:
http://www.fpdf.org/

Crear un fichero llamado tablas_pdf.php:
<?php
class PDF_MySQL_Table extends FPDF
{
var $ProcessingTable=false;
var $aCols=array();
var $TableX;
var $HeaderColor;
var $RowColors;
var $ColorIndex;

function Header()
{
    //Print the table header if necessary
    if($this->ProcessingTable)
        $this->TableHeader();
}

function TableHeader()
{
    $this->SetFont('Arial','B',12);
    $this->SetX($this->TableX);
    $fill=!empty($this->HeaderColor);
    if($fill)
        $this->SetFillColor($this->HeaderColor[0],$this->HeaderColor[1],$this->HeaderColor[2]);
    foreach($this->aCols as $col)
        $this->Cell($col['w'],6,$col['c'],1,0,'C',$fill);
    $this->Ln();
}

function Row($data)
{
    $this->SetX($this->TableX);
    $ci=$this->ColorIndex;
    $fill=!empty($this->RowColors[$ci]);
    if($fill)
        $this->SetFillColor($this->RowColors[$ci][0],$this->RowColors[$ci][1],$this->RowColors[$ci][2]);
    foreach($this->aCols as $col)
        $this->Cell($col['w'],5,$data[$col['f']],1,0,$col['a'],$fill);
    $this->Ln();
    $this->ColorIndex=1-$ci;
}

function CalcWidths($width,$align)
{
    //Compute the widths of the columns
    $TableWidth=0;
    foreach($this->aCols as $i=>$col)
    {
        $w=$col['w'];
        if($w==-1)
            $w=$width/count($this->aCols);
        elseif(substr($w,-1)=='%')
            $w=$w/100*$width;
        $this->aCols[$i]['w']=$w;
        $TableWidth+=$w;
    }
    //Compute the abscissa of the table
    if($align=='C')
        $this->TableX=max(($this->w-$TableWidth)/2,0);
    elseif($align=='R')
        $this->TableX=max($this->w-$this->rMargin-$TableWidth,0);
    else
        $this->TableX=$this->lMargin;
}

function AddCol($field=-1,$width=-1,$caption='',$align='L')
{
    //Add a column to the table
    if($field==-1)
        $field=count($this->aCols);
    $this->aCols[]=array('f'=>$field,'c'=>$caption,'w'=>$width,'a'=>$align);
}

function Table($query,$prop=array())
{
    //Issue query
    $res=mysql_query($query) or die('Error: '.mysql_error()."<BR>Query: $query");
    //Add all columns if none was specified
    if(count($this->aCols)==0)
    {
        $nb=mysql_num_fields($res);
        for($i=0;$i<$nb;$i++)
            $this->AddCol();
    }
    //Retrieve column names when not specified
    foreach($this->aCols as $i=>$col)
    {
        if($col['c']=='')
        {
            if(is_string($col['f']))
                $this->aCols[$i]['c']=ucfirst($col['f']);
            else
                $this->aCols[$i]['c']=ucfirst(mysql_field_name($res,$col['f']));
        }
    }
    //Handle properties
    if(!isset($prop['width']))
        $prop['width']=0;
    if($prop['width']==0)
        $prop['width']=$this->w-$this->lMargin-$this->rMargin;
    if(!isset($prop['align']))
        $prop['align']='C';
    if(!isset($prop['padding']))
        $prop['padding']=$this->cMargin;
    $cMargin=$this->cMargin;
    $this->cMargin=$prop['padding'];
    if(!isset($prop['HeaderColor']))
        $prop['HeaderColor']=array();
    $this->HeaderColor=$prop['HeaderColor'];
    if(!isset($prop['color1']))
        $prop['color1']=array();
    if(!isset($prop['color2']))
        $prop['color2']=array();
    $this->RowColors=array($prop['color1'],$prop['color2']);
    //Compute column widths
    $this->CalcWidths($prop['width'],$prop['align']);
    //Print header
    $this->TableHeader();
    //Print rows
    $this->SetFont('Arial','',11);
    $this->ColorIndex=0;
    $this->ProcessingTable=true;
    while($row=mysql_fetch_array($res))
        $this->Row($row);
    $this->ProcessingTable=false;
    $this->cMargin=$cMargin;
    $this->aCols=array();
}
}

class PDF extends PDF_MySQL_Table
{

function Header()
{
    //Title
    $this->SetFont('Arial','',18);
    $this->Cell(0,6,'LISTADO DE USUARIOS',0,1,'C');
    $this->Ln(10);
    //Ensure table header is output
    parent::Header();
}
}
?>

EJEMPLO SENCILLO SIN TABLAS:
<?php
require('./fpdf/fpdf.php');
require('tablas_pdf.php');
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'¡Mi primera página pdf con FPDF!');
$pdf->Output();
?>

EJEMPLO CON TABLAS:
mysql_connect('localhost','root','');
mysql_select_db(bd_pruebas');
$pdf = new PDF();
$pdf->AddPage();
// Columnas Cabecera
$pdf->Table('select nombre_completo, username, email, nif from users');
$pdf->AddPage();
// Columnas cuerpo
$pdf->AddCol('nombre_completo',20,'','C');
$pdf->AddCol('username',20,'','C');
$pdf->AddCol('email',20,'','C');
$pdf->AddCol('nif',20,'','C');
$pdf->Output();