Streaming a PHP Barcode to and IMG tag

别来无恙 提交于 2019-12-13 00:53:51

问题


I have a unique problem where I have to use only HTML because of platform constraints to send out receipt emails.

The receipts need to have a CODE128 Barcode generated on them. Unfortunately because I can only use HTML in the email template I am looking to figure out how to create these barcodes and link them with an IMG tag.

So my current idea is to use the PDFCrowd php library on a different server and then create an IMG tag in the email template with the SRC being a GET Request URL to the other server with the php library on it.

Then I would generate html with inline css and convert that to an image and stream it back out.

Would the HTML Email Template IMG tag work with that type of URL Setup!?

...This is for a quick and dirty shopify solution.

<?php
require 'pdfcrowd.php';

try
{
    // create the API client instance
    $client = new \Pdfcrowd\HtmlToImageClient("username", "apikey");

    // configure the conversion
    $client->setOutputFormat("png");

    // create output file for conversion result
    $output_file = fopen("HelloWorld.png", "wb");

    // run the conversion and store the result into an image variable
    $image = $client->convertString("<html><body><h1>Hello World!</h1></body></html>");

    // write the image the into the output file
    fwrite($output_file, $image);

    // close the output file
    fclose($output_file);
}
catch(\Pdfcrowd\Error $why)
{
    // report the error to the standard error stream
    fwrite(STDERR, "Pdfcrowd Error: {$why}\n");
}

?>

回答1:


You can use a barcode generator that can be found on github here. I remember being in a similar need sometime back, downloaded the barcode generator and it got the job done.

Example 1:

Parameters:

  • Text: “0” (Default)
  • Size: “20” (Default)
  • Code Type: “Code128” (Default)
  • Orientation: “Horizontal” (Default)

HTML Source Code:

<img alt="testing" src="/code/barcode.php" />

Result:

Example 2:

Parameters:

  • Text: “testing”
  • Size: “20” (Default)
  • Code Type: “Code128” (Default)
  • Orientation: “Horizontal” (Default)
  • Print: “true”

HTML Source Code:

<img alt="testing" src="/code/barcode.php?text=testing&print=true" />

Result:

Example 3:

Parameters:

  • Text: “TESTING”
  • Size: “40”
  • Code Type: “Code39”
  • Orientation: “Horizontal” (Default)
  • Print: “true”

HTML Source Code:

<img alt="TESTING" src="/code/barcode.php?codetype=Code39&size=40&text=TESTING&print=true" />

Result:

More examples in the source link.

Source: David Scott Tufts



来源:https://stackoverflow.com/questions/49642144/streaming-a-php-barcode-to-and-img-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!