dvisbeen/knp-invoice

此软件包最新版本(dev-master)没有可用的许可证信息。

PDF发票生成

此软件包的官方仓库似乎已消失,因此已将其冻结。

dev-master 2016-06-06 09:29 UTC

This package is not auto-updated.

Last update: 2022-04-16 04:41:17 UTC


README

要安装此库,请调用

$ php composer.phar install

为您提供生成html/pdf发票的能力。

<?php

// Set up Invoice model (required to do anything)
$invoice = new Knp\Invoice\Model\Invoice();

// Lets set up seller data now
$seller = new Knp\Invoice\Model\Seller();
$seller->setName('KnpLabs France');
// Add address: street, city, zip-code, country
$seller->setAddress('11 RUE KERVEGAN', 'NANTES', '44000', 'France');
// Address of logotype image
// $seller->setLogo('images/logo.png');

// Now add seller to invoice
$invoice->setSeller($seller);

// Lets set up buyer data now
$buyer = new Knp\Invoice\Model\Buyer();
$buyer->setName('Marek Nowak');
// Add address: street, city, zip-code, country
$buyer->setAddress('Kozia 5', 'Kozia Wólka', '00-666', 'Poland');

// Now add buyer to invoice
$invoice->setBuyer($buyer);

/*
// Customer has coupon ? Let's add it
$coupon = new Knp\Invoice\Model\Coupon;
$coupon->setName('Christmas Holidays');
$coupon->setValue(66.6);
// Coupon will expire at given date
// $coupon->setExpireAt('2011-12-25');
// Now add coupon to invoice
$invoice->setCoupon($coupon);
*/

// Customer already paid something ? Let's add this
$invoice->setPaidAmount(100);

// Now let's setup taxes
$taxTPS = new Knp\Invoice\Model\Tax;
$taxTPS->setName('TPS');
$taxTPS->setValue(5);

$taxTVQ = new Knp\Invoice\Model\Tax('TVQ', 8);

// Here we setup and add some random entries to invoice
for ($i = 0; $i < 5; $i++) {
    $entry = new Knp\Invoice\Model\Entry();
    $entry->setDescription('Development ' . mt_rand(1, 9));
    // Netto price per unit
    $entry->setUnitPrice(mt_rand(100, 500));
    // Quantity of given entry
    $entry->setQuantity(mt_rand(1, 4));

    // Entry can have up to two taxes added (i.e. Canada)
    // If no taxes add by using this method, there will be used 0% default one
    $entry->addTax($taxTPS);
    $entry->addTax($taxTVQ);

    // And let's add entry to invoice
    $invoice->addEntry($entry);
}

// Let's select generator
// $content = new Knp\Invoice\Generators\Snappy;
$content = new Knp\Invoice\Generators\Twig;
$content->generate($invoice);

// $content->generateAndSave($invoice); # You can generate and save invoice instead of returning to browser
// or
// $content->generate($invoice, null, 'list'); # generate just list of entries

echo $content;

创建自己的模板

现在您知道了如何生成发票,以下是一个使用自己的Twig模板的示例

{# MyDir/invoice.html.twig #}

{% extends 'base.html.twig' %}
{# This will lookup for this template in: "MyDir" and if not found in default Knp\Invoice theme dir #}

{% block content %}
<table>
    <thead>
    <tr>
        <th class="col1">Entry</th>
        <th class="col3">Price</th>
        <th class="col4">Quantity</th>
        <th class="col5">Line Total</th>
    </tr>
    </thead>

    <tbody>
    {%- for entry in invoice.entries %}
    <tr>
        <td>#{{ loop.index }}</td>
        <td class="col3">{{ entry.unitPrice }}</td>
        <td class="col4">{{ entry.quantity }}</td>
        <td class="col5">{{ entry.totalPrice }}</td>
    </tr>
    {%- endfor %}
    </tbody>
</table>
{% endblock %}
<?php

$content = new Knp\Invoice\Generators\Twig;
$content->setTemplate('MyDir/invoice.html.twig');
$content->generate($invoice);

echo $content;

启动测试套件

bin/vendors.sh

然后只需调用

phpunit

待办事项

https://trello.com/board/knpinvoicebundle/4ec25be9e900feed372d9661