【问题标题】:Coldfusion cfimage barcode generator (generating multiple barcodes)Coldfusion cfimage 条码生成器(生成多个条码)
【发布时间】:2017-02-03 06:52:54
【问题描述】:

这个脚本运行良好。我遇到的问题是试图让它每页生成多个条形码。它只会生成第一个。其余的都是空白图像。

<cfoutput>
<cfset x = 0>
<cfloop index="price_tag" from="1" to="#arrayLen( session.tags )#">
 <cfset x = x + 1>
<cfscript>
code128= createobject("java","com.lowagie.text.pdf.Barcode128");
code128.setCodeType(code128.CODE128);
/* Set the code to generate */
code128.setCode("#Session.tags[price_tag].itemnum#");
color =  createobject("java","java.awt.Color");
image = code128.createAwtImage(color.black, color.white);
bufferedImage = createObject("java", "java.awt.image.BufferedImage");
bufferedImageType = bufferedImage.TYPE_BYTE_GRAY;
bufferedImage = bufferedImage.init(image.getWidth(JavaCast("null", "")),image.getHeight(JavaCast("null", "")), bufferedImageType);
graphics2D = bufferedImage.createGraphics();
graphics2D.drawImage(image,0,0,JavaCast("null", ""));
barcodeImage = imageNew(bufferedImage);
</cfscript>   


         <div style="margin:0px; padding:0px;">
            <div style="font-size:42px; font-weight:bold; margin-bottom:0px; margin-right: 10px; margin-top: -5px;">#Session.tags[price_tag].item_name#</div>
            <div style="font-size:32px; margin-bottom:0px; margin-top:-7px; margin-right: 10px;">#Session.tags[price_tag].item_brand#</div>
           <div style="font-size:24px; margin-bottom:0px; margin-top:-7px;">#Session.tags[price_tag].item_unit_size# #Session.tags[price_tag].item_unit_type# </div>
            <div style="font-size:120px; font-weight:bolder; margin-top:-55px; margin-right: 10px;" align="right">#DollarFormat(Session.tags[price_tag].item_retail)#</div>
            <div style="font-size:30px; font-weight:bolder; margin-top:-30px; margin-right: 10px;" align="right"> #Session.tags[price_tag].item_sold_by#</div>
            <div style="font-size:30px; margin-top:-35px; margin-left 10px;" align="left">#Session.tags[price_tag].itemnum#</div>
            <div style=" position:absolute; margin-left: 10px; margin-top: 100px;"><cfimage action="writeToBrowser" source="#barcodeImage#" format="png" quality="1" width="180px" overwrite = "yes"></div>
         </div>   
   <cfif #arrayLen( session.tags )# / #x# NEQ 1>                            
   <cfdocumentitem type="pagebreak"/> 
   </cfif>
</cfloop>
</cfoutput>

【问题讨论】:

  • 在脚本中输出x、价格标签等各种变量。这将帮助您确定正在发生的事情。可能与您的问题无关,但您可以在开始循环之前创建 java 对象并在循环中使用它们。
  • 不使用&lt;cfset x = x + 1&gt;,而是将其跟踪为#price_tag#。处理的变量少了一个。
  • 尝试取出overwrite = "yes"。当你action="writeToBrowser" 时,我认为你不需要那个。
  • CFDocument 总是有点古怪。我怀疑可能与此有关:&lt;div style=" position:absolute; 如果删除它,就会出现条形码。

标签: coldfusion cfdocument cfimage


【解决方案1】:

这可能是因为您在 cfdocument 中的循环中使用了action="writeToBrowser"。它不会进入浏览器。

尝试编写真实文件,然后链接到它们。我已经做到了,并且知道它有效。确保在该循环中使用不同的文件名。

如果您打算反复运行此例程,则使用相同的文件名是安全的。但是在您的代码中,使用 url var 禁用图像 src 的缓存:

<cfoutput>
<img src="/media/cms/code-#price_tag#.png?rand=#randRange(0,1000)#" width="180"/>
</cfoutput>

【讨论】:

  • 它不会进入浏览器。 这真的不应该有什么不同。最终action=writeToBrowser 完全按照您的建议进行操作:创建一个物理文件并输出一个普通的 标记,其中包含指向该文件的 URL。所以它应该按原样工作。
  • 只是阅读这个:。 stackoverflow.com/questions/17372320/… 可能与生成的路径名有关。创建一个真正的文件并明确链接到它可以解决这个可能的问题。
  • 是的,如果是路径问题,您通常会看到一个红色的“X”。在这种情况下,结果是什么都没有。所以这可能不是同一个问题。看似空白的图像表明图像放置在错误的位置,或者可能没有完全呈现。由于删除position: absolute 似乎在一些快速测试中起作用,我猜这是cfdocument 如何解析html 的“怪癖”。 (旁注:使用相同的文件名是安全的 - 在多线程应用程序中重复使用文件名是不安全的,因为一个线程可能会覆盖另一个正在使用的文件) .
猜你喜欢
  • 1970-01-01
  • 2017-06-29
  • 1970-01-01
  • 1970-01-01
  • 2020-02-06
  • 1970-01-01
  • 1970-01-01
  • 2018-10-10
  • 2012-03-30
相关资源
最近更新 更多