🌐
PDF-LIB
pdf-lib.js.org
PDF-LIB · Create and modify PDF documents in any JavaScript environment.
<html> <head> <meta charset="utf-8" /> <script src="https://unpkg.com/pdf-lib"></script> </head> <body> <iframe id="pdf" style="width: 100%; height: 100%;"></iframe> </body> <script> createPdf(); async function createPdf() { const pdfDoc = await PDFLib.PDFDocument.create(); const page = pdfDoc.addPage([350, 400]); page.moveTo(110, 200); page.drawText('Hello World!'); const pdfDataUri = await pdfDoc.saveAsBase64({ dataUri: true }); document.getElementById('pdf').src = pdfDataUri; } </script> </html> Save this snippet as an HTML file and load it in your browser to get up and running with pdf-lib as quickly as possible.
API
Ƭ AppearanceMapping: object · Defined in api/form/appearances.ts:88
Help
Please take a look at our GitHub issues and see if your question has already been answered. If not, please create your own issue and we will respond as soon as possible!
🌐
PDFlib
pdflib.com › pdflib-cookbook
PDFlib: PDFlib Cookbook
All other Cookbooks can be used with the PDFlib product. Examples for the pCOS interface are presented in the pCOS Cookbook; they require PDFlib+PDI or PPS, or another product which includes the pCOS interface, i.e.
🌐
CodeSandbox
codesandbox.io › examples › package › pdf-lib
pdf-lib examples - CodeSandbox
Use this online pdf-lib playground to view and fork pdf-lib example apps and templates on CodeSandbox.
🌐
PDFlib
pdflib.com › fileadmin › pdflib › pdf › manuals › PDFlib-9.0.2-tutorial.pdf pdf
ABC PDFlib, PDFlib+PDI, PPS A library for generating PDF on the fly
The PDFlib Cookbook is a collection of PDFlib coding fragments for · solving specific problems. Most Cookbook examples are available for Java and PHP, but
🌐
CodeSandbox
codesandbox.io › examples › package › pdflib
pdflib examples - CodeSandbox
Use this online pdflib playground to view and fork pdflib example apps and templates on CodeSandbox.
🌐
GitHub
github.com › Hopding › pdf-lib
GitHub - Hopding/pdf-lib: Create and modify PDF documents in any JavaScript environment
For example: // NPM module import { PDFDocument, rgb } from 'pdf-lib'; // UMD module var PDFDocument = PDFLib.PDFDocument; var rgb = PDFLib.rgb; pdf-lib relies upon a sister module to support embedding custom fonts: @pdf-lib/fontkit.
Starred by 8.1K users
Forked by 829 users
Languages   TypeScript 80.9% | HTML 9.9% | JavaScript 8.5% | Objective-C 0.3% | CSS 0.2% | Starlark 0.1% | Java 0.1%
🌐
GitHub
github.com › Distrotech › PDFlib-Lite
GitHub - Distrotech/PDFlib-Lite: http://www.pdflib.com/download/free-software/pdflib-lite-7/
The mini samples are mainly useful for testing your PDFlib installation, and for getting a very quick overview of writing PDFlib applications. The hello, pdfclock, chartab, and image examples work with PDFlib or PDFlib Lite.
Starred by 32 users
Forked by 15 users
Languages   C 84.8% | Shell 2.8% | Java 2.6% | PHP 2.4% | C++ 2.0% | Perl 1.4%
🌐
GitHub
github.com › evosys21 › pdflib › blob › main › examples › Tcpdf › example-table-1-overview.php
pdflib/examples/Tcpdf/example-table-1-overview.php at main · evosys21/pdflib
* Pdf Advanced Table - Example · */ · require_once __DIR__ . '/autoload.php'; · use EvoSys21\PdfLib\Multicell; use EvoSys21\PdfLib\Examples\Tcpdf\PdfFactory; use EvoSys21\PdfLib\Examples\Tcpdf\PdfSettings; · //get the PDF object · $pdf = PdfFactory::newPdf('table'); ·
Author   evosys21
🌐
PDFlib
pdflib.com › documentation › manuals
PDFlib: Manuals
All PDFlib GmbH distribution packages include English product documentation. Therefore you already have a manual on your local disk after unpacking or installing a package.
Top answer
1 of 2
2

I know the easiest solution would be to use a table, but I can't because the PDF has to be accessible and they told me that

who said that? This statement is a bit too general for now. Also, your content is not accessible in any way, because you achieve this in the PDF only through Tagged PDF.

PDFlib can be used to create PDF/UA (i.e. Tagged PDF) which can be used to create accessible PDFs.

The easiest way would be to follow the PDFlib 10 Coobkook example https://www.pdflib.com/pdflib-cookbook/pdfua/table_pdfua1/php/ and fill one table cell after the other with a textflow.

Then PDFlib 10 will generate the table tagging for you, and you will have a nice accessible PDF. (See also PDFlib 10 Tutorial, Chapter 11.2.1 "Automatic Table Tagging"). The tutorial is included in the PDFlib 10 package, as well available on the download page.

For PDF/UA please also refer to Chapter 11.4.1 "The PDF/UA-1 Standard" in the PDFlib 10 Tutorial. Your code fragment already shows that you must at least change the font, because the font must be embedded. In your case you are using the PDF Latin Core font Helvetica which is not embedded. Therefore I recommend to use a font that you have and like. As an example you could of course use the NotoSerif-Regular used in the PDFlib examples.

if you want to stick with your solution, you could first determine the length for each key entry. You can do this via info_textline() with the same font options as when placing the text with fit_textline(). You could use this to determine the maximum length and then adjust the llx position for the textflows accordingly. I would not use leftindent, but pass different X values for fit_textline() and fit_texfflow().

Also, I would get the end position with info_textflow(), not with get_option("texty").

I think you are always better off with a PDFlib table. And if you really want to create Tagged PDF, that is also possible with PDFlib 10.

One comment about your used options:

$label_optlist = "fontname={Helvetica} fontsize=7 encoding=utf8 fakebold=true leftindent=0%";

encoding=utf8 is not valid. The correct option value is "encoding=unicode". You can set $pdf->set_optin("stringformat=utf8"); when your input content is UTF-8. In PDFlib 10 stringformat=utf8 and encoding=unicode is default and can be omitted.

2 of 2
0

I would like to answer your questions about the above answer in a new one. There you can format better.

Instead of leftindent you can also simply move your x position of the fit_textflow(). Hopefully that makes your code easier to understand.

About using encoding=utf8: I always get an error message then. Which PDFlib version are you using? (you can see in the phpinfo() output for example) Generally utf8 is not a valid keyword unless you have crafted and provided an encoding yourself. But that would not be recommended.

From the PDFlib 10 API Reference, chapter 4.1, table 4.1:

About embedding: you must provide the font files in the SearchPath, because PDFlib needs the font data at runtime. Please refer to the PDFlib 10 Tutorial, Chapter 3.1.4, and Chapter 6.3.4 "Searching for Fonts". In the supplied PDFlib examples the SearchPath is set to "../data", where you can find the resources needed in the examples. You might want to crib there.

Back to the table and accessibiltiy:

In general it is already possible to create a table without a header, but this will be flagged in accessibility checks. Depending on how important the topic is, you should implement the table accordingly. On the other hand, your output is currently not accessible either, so you will have a much easier time if you put everything in a table. I have attached a very simple PDF/UA table which is created with the following code. Maybe you can identify your problem more precisely.

<?php
/*
 *
 * Demonstrate automatic table tagging
 *
 * required software: PDFlib/PDFlib+PDI/PPS 10
 * required data: image file (dummy text created within the program)
 */

/* This is where the data files are. Adjust as necessary. */
$searchpath = dirname(__FILE__,3)."/input";
$title = "table_pdfua1";

$p = null;

try {
    $p = new pdflib();

    $tf = 0;
    $tbl = 0;
    $rowmax = 5;
    $colmax = 5;

    $llx = 50; $lly = 50; $urx = 550; $ury = 700;

    /* Dummy text for filling a cell with multi-line Textflow */
    $tf_text =
        "Sample text created with the Textflow feature in order to " .
        "create multiline content within a table cell.";

    /*
     * Set the search path for fonts and images etc.
     */
    $p->set_option(
        "errorpolicy=exception SearchPath={" . $searchpath . "}");
    

    if ($p->begin_document("", 
            "pdfua=PDF/UA-1 lang=en tag={tagname=Document}") == 0)
        throw new Exception("Error: " . $p->get_errmsg());

    $p->set_info("Creator", "PDFlib Cookbook");
    $p->set_info("Title", $title);
    
    /* Automatically create spaces between chunks of text */
    $p->set_option("autospace=true charref");

    /* -------------------- Add table cells -------------------- */
    $row = 1;
    $col = 1;
   
    for ($row; $row <= $rowmax; $row++) {
    /* ----- Simple text cell */
    $col = 1;

    $optlist = "colwidth = 100 fittextline={fontname=NotoSerif-Bold fontsize=10 position={left top}} margin=5";

    $tbl = $p->add_table_cell($tbl, $col, $row, "text " . $row, $optlist);

    /* ----- Multi-line text with Textflow */
    $col++;

    $optlist = "fontname=NotoSerif-Regular fontsize=10";

    $tf = $p->add_textflow(0, $tf_text, $optlist);

    $optlist = "colwidth=300 rowheight=8 margin=5 textflow=" . $tf . " fittextflow={firstlinedist=capheight}";

    $tbl = $p->add_table_cell($tbl, $col, $row, "", $optlist);
    }

    /* ---------- Place the table on one or more pages ---------- */

    /*
     * Loop until all of the table is placed; create new pages as long
     * as more table instances need to be placed.
     */
    do {
        $p->begin_page_ext(0, 0, "width=a4.width height=a4.height");
        
        $p->create_bookmark("Tagged table demo", "");
        
        $p->fit_textline("Name-Value pairs", 50, 750,
            "fontname=NotoSerif-Regular " .
            "fontsize=16 tag={tagname=H1}");
        
        /*
         * Shade every other $row; draw lines for all table cells. Add
         * "showcells showborder" to visualize cell borders
         */
        $optlist = "tag={tagname=Table Summary={key value tablese}} ";
        /* Place the table instance */
        $result = $p->fit_table($tbl, $llx, $lly, $urx, $ury, $optlist);

        if ($result == "_error")
            throw new Exception("Couldn't place table : "
                    . $p->get_errmsg());

        $p->end_page_ext("");
    }
    while ($result == "_boxfull");

    /* Check the $result; "_stop" means all is ok. */
    if (!$result == "_stop") {
        if ($result == "_error") {
            throw new Exception("Error when placing table: " 
                                        . $p->get_errmsg());
        }
        else {
            /*
             * Any other return value is a user exit caused by the
             * "return" option; this requires dedicated code to deal
             * with.
             */
            throw new Exception("User return found in Textflow");
        }
    }

    /* This will also delete Textflow handles used in the table */
    $p->delete_table($tbl, "");

    $p->end_document("");
    $buf = $p->get_buffer();
    $len = strlen($buf);

    header("Content-type: application/pdf");
    header("Content-Length: $len");
    header("Content-Disposition: inline; filename=" . $title . ".pdf");
    print $buf;
}
catch (PDFlibException $e) {
    echo("PDFlib exception occurred in" . $title . "sample:\n" .
        "[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " .
        $e->get_errmsg() . "\n");
    exit(1);
}
catch (Throwable $e) {
    echo($e);
    exit(1);
}

$p = 0;
?>
        

this sample is adapted from the PDFlib Cookbook "pdfua/table_pdfua1".

However, I suspect that the accessibility issue is not a real problem for you, so you could simply implement the whole thing with a PDFlib table. Just ask the person what the table and accessibility concerns are. Because if the real Tagged PDF (PDF/UA) is not an issue, many things become easier.

Find elsewhere
🌐
PDFlib
pdflib.com › pdflib-cookbook › general › starter_basic › php
starter_basic
$p->get_errmsg()); exit(1); } /* Page 1 */ $p->begin_page_ext(0, 0, "width=a4.width height=a4.height"); /* Use NotoSerif-Regular font with text format UTF-8 for placing the text * and demonstrate various options how to pass the UTF-8 text to PDFlib */ $optlist = "fontname={NotoSerif-Regular} fontsize=24"; /* Plain ASCII text */ $p->fit_textline("en: Hello!", 50, 700, $optlist); /* Unicode code points */ $p->fit_textline("gr: \u{0393}\u{03B5}\u{03B9}\u{03AC}!", 50, 650, $optlist); /* UTF-8 text */ $p->fit_textline("ru: Привет!", 50, 600, $optlist); /* PDFlib character reference */ $p->fit_textline("es: &#xA1;Hola!", 50, 550, $optlist .
🌐
Adamharvey
php.adamharvey.name › manual › en › pdf.examples-basic.php
PHP: Basic Usage Examples - Manual
$p->get_errmsg()); } $p->set_info("Creator", "hello.php"); $p->set_info("Author", "Rainer Schaaf"); $p->set_info("Title", "Hello world (PHP)!"); $p->begin_page_ext(595, 842, ""); $font = $p->load_font("Helvetica-Bold", "winansi", ""); $p->setfont($font, 24.0); $p->set_text_pos(50, 700); $p->show("Hello world!"); $p->continue_text("(says 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=hello.pdf"); print $buf; } catch (PDFlibException $e) { die("PDFlib exception occurred in hello sample:\n" .
🌐
University of Utah Mathematics Department
ftp.math.utah.edu › u › ma › hohn › linux › postscript › PDFlib.pdf pdf
PDFlib Reference Manual
these restrictions should be straightforward. For example, you don’t have to · think about opening a page first before closing it, etc. The main program · structure of a PDFlib application is as shown in the »Hello, world!« sample
🌐
Racad Tech Support
support.racadtech.com › support › solutions › articles › 36000013799-pdflib-getting-started-tutorial-quick-reference-guide
PDFlib Getting Started Tutorial + Quick Reference Guide : Racad Tech Support
https://www.pdflib.com/download/block-plugin/ A sample pdf file to practice on. PDF Lib documentation (word file). ePower quick reference guide (excel) Sea World tutorial. Attachments (1) zip · PDFLibStarti....zip 2.54 MB · Was this article helpful? No Yes · That’s Great!
🌐
GitHub
github.com › topics › pdflib
pdflib · GitHub Topics · GitHub
react javascript pdf privacy typescript sdk reactjs react-component form pdf-viewer react-pdf pdflib sdk-js pdf-lib pdf-editor pdf-sdk pdf-editor-react
🌐
Prideindustries
noboundaries.prideindustries.com › mis › PDFlib-manual.pdf pdf
PDFlib-manual.pdf
Example for PHP 5. The following sample uses the new exception handling and object ... Error handling in PHP 4. When a PDFlib exception occurs, a PHP exception is thrown.
🌐
PDFlib
pdflib.com
PDFlib: PDFlib GmbH Home
The PDFlib Cookbook with many helpful examples · PDFlib TET (Text and Image Extraction Toolkit) reliably extracts text, images and metadata from PDF documents. It is available in multiple flavors for different deployment scenarios.
🌐
PDFlib
pdflib.com › products › pdflib-family › overview
PDFlib: What is PDFlib?
PDFlib is a well-established developer component for generating and manipulating PDF documents. PDFlib’s main targets are dynamic PDF creation on a Web server and PDF creation in any kind of server, desktop or mobile application. You can use PDFlib to dynamically create PDF documents from ...
🌐
GitHub
github.com › evosys21 › pdflib › blob › main › examples › Tcpdf › example-multicell-1-overview.pdf
pdflib/examples/Tcpdf/example-multicell-1-overview.pdf at main · evosys21/pdflib
Fpdf, TCPDF, tFpdf - Advanced Multicell and Table. Contribute to evosys21/pdflib development by creating an account on GitHub.
Author   evosys21
🌐
PDF-LIB
pdf-lib.js.org › docs › api
pdf-lib · PDF-LIB
Ƭ AppearanceMapping: object · Defined in api/form/appearances.ts:88