PHP Velho Oeste 2024

Basic Usage Examples

La mayoría de las funciones son bastante fáciles de usar. La parte más difícil es probablemente la de crear su primer documento PDF. El siguiente ejemplo debería ayudarle a empezar. Está desarrolado para PHP 4 y crea el archivo hola.pdf con una página. Define algunos contenidos de campos de información del documento, carga la fuente Helvetica-Bold e imprime el texto "¡Hola mundo! (dice PHP)".

Ejemplo #1 Ejemplo de Hola Mundo con la distribucion PDFlib para PHP 4

<?php
$p 
PDF_new();

/*  abrir un nuevo archivo PDF; insertar un nombre de archivo para crear el PDF en disco */
if (PDF_begin_document($p"""") == 0) {
    die(
"Error: " PDF_get_errmsg($p));
}

PDF_set_info($p"Creator""hola.php");
PDF_set_info($p"Author""Rainer Schaaf");
PDF_set_info($p"Title""¡Hola mundo (PHP)!");

PDF_begin_page_ext($p595842"");

$font PDF_load_font($p"Helvetica-Bold""winansi""");

PDF_setfont($p$font24.0);
PDF_set_text_pos($p50700);
PDF_show($p"¡Hola mundo!");
PDF_continue_text($p"(dice PHP)");
PDF_end_page_ext($p"");

PDF_end_document($p"");

$buf PDF_get_buffer($p);
$len strlen($buf);

header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=hola.pdf");
print 
$buf;

PDF_delete($p);
?>

El siguiente ejemplo viene con la distribución de PDFlib para PHP 5. Usa las nuevas características de manejo de excepciones y encapsulación de objetos disponibles en PHP 5. Crea el archivo hola.pdf con una página. Define algunos contenidos de campos de información del documento, carga la fuente Helvetica-Bold e imprime el texto "¡Hola mundo! (dice PHP)".

Ejemplo #2 Ejemplo de Hola Mundo con la distribucion PDFlib para PHP 5

<?php

try {
    
$p = new PDFlib();

    
/*  abrir un nuevo archivo PDF; insertar un nombre de archivo para crear el PDF en disco */
    
if ($p->begin_document("""") == 0) {
        die(
"Error: " $p->get_errmsg());
    }

    
$p->set_info("Creator""hola.php");
    
$p->set_info("Author""Rainer Schaaf");
    
$p->set_info("Title""¡Hola mundo (PHP)!");

    
$p->begin_page_ext(595842"");

    
$font $p->load_font("Helvetica-Bold""winansi""");

    
$p->setfont($font24.0);
    
$p->set_text_pos(50700);
    
$p->show("¡Hola mundo!");
    
$p->continue_text("(dice PHP)");
    
$p->end_page_ext("");

    
$p->end_document("");

    
$buf $p->get_buffer();
    
$len strlen($buf);

    
header("Content-type: application/pdf");
    
header("Content-Length: $len");
    
header("Content-Disposition: inline; filename=hola.pdf");
    print 
$buf;
}
catch (
PDFlibException $e) {
    die(
"Ocurrió un Excepción PDFlib en el ejemplo hola:\n" .
    
"[" $e->get_errnum() . "] " $e->get_apiname() . ": " .
    
$e->get_errmsg() . "\n");
}
catch (
Exception $e) {
    die(
$e);
}
$p 0;
?>

add a note add a note

User Contributed Notes 1 note

up
1
matt at kynx dot org
12 years ago
Be careful with your Content-Length if you're using multi-byte function overloading (http://www.php.net/manual/en/mbstring.overload.php). You will want to do:

<?php
$len
= mb_strlen($buf, 'ASCII');
?>

Otherwise your PDF will be truncated and unreadable. Just cost me a sunny afternoon :-/
To Top