Giter Site home page Giter Site logo

goldyteam / pdf-from-json-template Goto Github PK

View Code? Open in Web Editor NEW

This project forked from helderrscorreia/pdf-from-json-template

0.0 0.0 0.0 326 KB

Generate PDF documents from a JSON defined structure

License: GNU General Public License v3.0

PHP 100.00%

pdf-from-json-template's Introduction

pdf-from-json-template

Generate PDF documents from a JSON defined structure

Requirements

To use this library you must have TCPDF included in your PHP project: https://github.com/tecnickcom/TCPDF

// include TCPDF
include_once('./TCPDF_main/tcpdf_import.php');

Setup

include_once('PDFParser.php');

Usage

// load JSON template from file or variable
$JSONTemplateString = file_get_contents('samples/simple-a4.json');

// filename to be generated
$outputFilename = "document.pdf";

// instantiate PDFParser
$pdfParser = new PDFParser($JSONTemplateString, $outputFilename);

// OPTIONAL: set data array to be used
$pdfParser->setData([
  "person-name" => "John Silver",
  "person-address" => "Yellow Street 34",
  "person-zipcode" => "34500",
  "person-city" => "Liverpool"
]);

// render PDF file
$pdfParser->render();

Components

Page

Page is the only required component, and must always be placed at the beginning of the JSON structure.

{
  "type": "page",
  "options": {
    "format": "A4" // The values can be A4,A5,... and also [sizeX,sizeY],
    "units": "mm",
    "orientation": "P" // "P" for portrait or "L" for landscape
  }
}

Text

{
  "type": "text",
  "text": "Hello world!" // string to be shown
  "data": "person_name" // data array index
  "options": {
    "x": 10,
    "y": 10,
    "font-size": 12,
    "font-family": "times",
    "color": "#ff00ff",
    "text-decoration": "B", // B-Bold, I-Italic, empty-none, can be used together
    "rotation": 0
  }
}

Cell

{
  "type": "cell",
  "text": "Hello world!" // string to be shown
  "data": "person_name" // data array index
  "options": {
    "x": 10,
    "y": 10,
    "width": 50,
    "height": 5,
    "border": 0, // 0-disable, 1-enable
    "multiline": false,
    "font-size": 12,
    "font-family": "times",
    "color": "#ff00ff",
    "text-align": "L" // L-left, R-right, C-center, J-justified
    "text-decoration": "B", // B-Bold, I-Italic, empty-none, can be used together
    "rotation": 0 // degrees
  }
}

Image

{
  "type": "image",
  "src": "https://www.yoursite.com/image.png",
  "options": {
    "x": 10,
    "y": 10,
    "width": 50,
    "height": 5
  }
}

Line

{
  "type": "line",
  "options": {
    "x1" => 10,
    "y1" => 10,
    "x2" => 40,
    "y2" => 10,
    "width" => 0.1 // line width
    "color" => [0,0,0] // RGB array
   }
}

Box

{
  "type": "box",
  "options": {
    "x": 10,
    "y": 10,
    "width": 50,
    "height": 50,
    "border-width": 0.1,
    "border-color": [0,0,0],
    "fill-color": [255,255,255]
  }
}

QRCode

{
  "type": "box",
  "content": "www.yoursite.com",
  "data": "variable.field" // data array variable
  "options": {
    "x": 10,
    "y": 10,
    "width": 50,
    "border": true,
    "vpadding": "auto",
    "hpadding": "auto",
    "fgcolor": [0,0,0],
    "bgcolor": [255,255,255],
    "module_width": 1,
    "module_height": 1
  }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.