在A4纸上生成具有40个QR码的QR码表,从而在不同系统上产生不同的对齐

时间:2018-04-26 12:16:17

标签: java xml qr-code xlsx

我正在使用Java NetBeans application,用户可以在PDF文件上生成40张QR码,因为我正在使用Apache FOP,用于设计/格式化QR码PDF,我正在使用xlsx文件,用于动态数据xml文件。

问题是在A4纸上打印PDF后,它会给出不同系统的不同分配。

有没有人可以告诉可能是什么问题,因为这些条形码将被打印在标签纸上,因为它们将被用作贴纸,不同的对齐导致QR码到达指定区域之外。所有系统的对齐应保持不变。

任何帮助将不胜感激,我将提供更多信息。

创建PDF文件的方法

public void createPdfSheet() throws IOException, TransformerException, FOPException, SAXException{        
    addWardNameToXml(wardName_QR);

    File files = new File("pdf");
    if (!files.exists()) {
        if (files.mkdir()) {
            System.out.println("Directory is created!");
        } else {
            System.out.println("Failed to create directory!");
        }
    }
       String pdfFile = "";

        try {
        System.out.println("FOP ExampleXML2PDF\n");
        System.out.println("Preparing...");
        // Setup input and output files
        File xmlfile = new File("pdf\\", "document.xml");
        File xsltfile = new File("pdf\\", "template.xsl");
        File pdffile = new File("pdf\\", "codeland.pdf");
        OutputStream file;
        try{
            file = new FileOutputStream(new File("pdf\\codeland.pdf"));
            pdfFile = "pdf\\codeland.pdf";
            pdffile = new File("pdf/", "codeland.pdf");
        }catch(FileNotFoundException fof){
            pdfFile = "pdf\\codeland"+new Date().getTime()+".pdf";
            file = new FileOutputStream(new File(pdfFile));
            pdffile = new File("pdf/", "codeland"+new Date().getTime()+".pdf");    
        }          

        System.out.println("Input: XML (" + xmlfile + ")");
        System.out.println("Stylesheet: " + xsltfile);
        System.out.println("Output: PDF (" + pdffile + ")");
        System.out.println();
        System.out.println("Transforming...");
        // configure fopFactory as desired
        FopFactory fopFactory = FopFactory.newInstance(new File("fop-2.2\\fop\\conf\\fop.xconf"));
        //new File("src/fop.xconf");
        FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
        // configure foUserAgent as desired
        // Setup output
        OutputStream out = new java.io.FileOutputStream(pdffile);
        out = new java.io.BufferedOutputStream(out);
        try {
            // Construct fop with desired output format
            Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF,
                    foUserAgent, out);
            // Setup XSLT
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory
                    .newTransformer(new StreamSource(xsltfile));
            // Set the value of a <param> in the stylesheet
            transformer.setParameter("versionParam", "2.0");
            // Setup input for XSLT transformation
            Source src = new StreamSource(xmlfile);
            // Resulting SAX events (the generated FO) must be piped through
            // to FOP
            Result res = new SAXResult(fop.getDefaultHandler());
            // Start XSLT transformation and FOP processing
            transformer.transform(src, res);
        } finally {
            out.close();
        }
        System.out.println("Success!");

        //pdfFile = "pdf\\codeland.pdf";
        file.close();
        File fileForDesktop = new File(pdfFile);
        if (pdfFile.toString().endsWith(".pdf")) {
            Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + pdfFile);
        } else {
           //For cross platform use
            Desktop desktop = Desktop.getDesktop();
            desktop.open(fileForDesktop);
        }
    } catch (Exception e) {
        System.out.print("ERROR!!");
        e.printStackTrace(System.err);
        System.exit(-1);
    }

}

xlsx文件:

     <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="/">
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
        <fo:layout-master-set>
            <fo:simple-page-master master-name="simple"
                page-height="11.69in" page-width="8.27in" margin-top="0.0in"
                margin-bottom="0.0in" margin-left="0.0in" margin-right="0.0in">
                <fo:region-body margin-top="0cm" margin-bottom="0cm" />
                <fo:region-before extent="0cm" overflow="hidden" />
                <fo:region-after extent="0cm" overflow="hidden" />
            </fo:simple-page-master>
        </fo:layout-master-set>
        <fo:page-sequence master-reference="simple"
            initial-page-number="1">
            <fo:static-content flow-name="xsl-region-before">
                <fo:block font-size="0.0pt" font-family="serif"
                    padding-after="0.0pt" space-before="0.0pt" text-align="center"
                    border-bottom-style="solid" border-bottom-width="0.0pt">
                    <xsl:text></xsl:text>
                </fo:block>
            </fo:static-content>
            <fo:static-content flow-name="xsl-region-after">
                <fo:block font-size="0.0pt" font-family="sans-serif"
                    padding-after="0.0pt" space-before="0.0pt" text-align="center"
                    border-top-style="solid" border-bottom-width="0.0pt">
                    <xsl:text>P</xsl:text>
                    <fo:page-number />
                </fo:block>
            </fo:static-content>
            <fo:flow flow-name="xsl-region-body">
                <xsl:apply-templates select="data" />
            </fo:flow>
        </fo:page-sequence>
    </fo:root>
</xsl:template>
<xsl:template match="data">
    <fo:block text-align="center">
        <fo:table table-layout="fixed" width="100%">
            <fo:table-column column-width="20%" />
            <fo:table-column column-width="20%" />
            <fo:table-column column-width="20%" />
            <fo:table-column column-width="20%" />
            <fo:table-column column-width="20%" />

            <fo:table-body>
                <fo:table-row keep-together.within-page="always" height="1.4in">
                    <fo:table-cell height="1.03in" padding-bottom="0.05in">
                        <fo:block  font-size="7pt" font-family="sans-serif"
                                background-color="white" color="black" text-align="center" >
                            <xsl:apply-templates select="hospital" />
                        </fo:block>
                        <fo:block>
                            <fo:external-graphic  height="auto" width="auto" content-height="1.03in"
                                                    content-width="1.03in" src="url(file:pdf/QR1.png)" />
                        </fo:block>                                             
                        <fo:block  font-size="7pt" font-family="sans-serif"
                                background-color="white" color="black" text-align-last="center" >                                       
                            <xsl:apply-templates select="floor" />
                            <fo:leader leader-pattern="space" />
                            <fo:external-graphic  height="0.1in" width="0.1in" content-height="0.1in"
                                                    content-width="0.1in" src="url(file:pdf/dot.png)" />
                        </fo:block>                             
                    </fo:table-cell>
                    <fo:table-cell height="1.03in">
                        <fo:block  font-size="7pt" font-family="sans-serif"
                                background-color="white" color="black" text-align="center" >
                            <xsl:apply-templates select="hospital" />
                        </fo:block> 
                        <fo:block>
                            <fo:external-graphic  height="auto" width="auto" content-height="1.03in"
                                                    content-width="1.03in" src="url(file:pdf/QR2.png)" />
                        </fo:block>                     
                        <fo:block  font-size="7pt" font-family="sans-serif"
                                background-color="white" color="black" text-align-last="center" >                                       
                            <xsl:apply-templates select="floor" />
                            <fo:leader leader-pattern="space" />
                            <fo:external-graphic  height="0.1in" width="0.1in" content-height="0.1in"
                                                    content-width="0.1in" src="url(file:pdf/dot.png)" />
                        </fo:block>         
                    </fo:table-cell>
                    <fo:table-cell height="1.03in">
                        <fo:block  font-size="7pt" font-family="sans-serif"
                                background-color="white" color="black" text-align="center" >
                            <xsl:apply-templates select="hospital" />
                        </fo:block>
                        <fo:block>
                            <fo:external-graphic  height="auto" width="auto" content-height="1.03in"
                                                    content-width="1.03in" src="url(file:pdf/QR3.png)" />
                        </fo:block>                         
                        <fo:block  font-size="7pt" font-family="sans-serif"
                                background-color="white" color="black" text-align-last="center" >                                       
                            <xsl:apply-templates select="floor" />
                            <fo:leader leader-pattern="space" />
                            <fo:external-graphic  height="0.1in" width="0.1in" content-height="0.1in"
                                                    content-width="0.1in" src="url(file:pdf/dot.png)" />
                        </fo:block>         
                    </fo:table-cell>
                    <fo:table-cell height="1.03in">
                        <fo:block  font-size="7pt" font-family="sans-serif"
                                background-color="white" color="black" text-align="center" >
                            <xsl:apply-templates select="hospital" />
                        </fo:block>
                        <fo:block>
                            <fo:external-graphic  height="auto" width="auto" content-height="1.03in"
                                                    content-width="1.03in" src="url(file:pdf/QR4.png)" />
                        </fo:block>                     
                        <fo:block  font-size="7pt" font-family="sans-serif"
                                background-color="white" color="black" text-align-last="center" >                                       
                            <xsl:apply-templates select="floor" />
                            <fo:leader leader-pattern="space" />
                            <fo:external-graphic  height="0.1in" width="0.1in" content-height="0.1in"
                                                    content-width="0.1in" src="url(file:pdf/dot.png)" />
                        </fo:block>         
                    </fo:table-cell>
                    <fo:table-cell height="1.03in">
                        <fo:block  font-size="7pt" font-family="sans-serif"
                                background-color="white" color="black" text-align="center" >
                            <xsl:apply-templates select="hospital" />
                        </fo:block>
                        <fo:block>
                            <fo:external-graphic  height="auto" width="auto" content-height="1.03in"
                                                    content-width="1.03in" src="url(file:pdf/QR5.png)" />
                        </fo:block>                     
                        <fo:block  font-size="7pt" font-family="sans-serif"
                                background-color="white" color="black" text-align-last="center" >                                       
                            <xsl:apply-templates select="floor" />
                            <fo:leader leader-pattern="space" />
                            <fo:external-graphic  height="0.1in" width="0.1in" content-height="0.1in"
                                                    content-width="0.1in" src="url(file:pdf/dot.png)" />
                        </fo:block>         
                    </fo:table-cell>
                </fo:table-row>     
                <fo:table-row keep-together.within-page="always" height="1.4in">
                    <fo:table-cell height="1.03in">
                        <fo:block  font-size="7pt" font-family="sans-serif"
                                background-color="white" color="black" text-align="center" >
                            <xsl:apply-templates select="hospital" />
                        </fo:block>
                        <fo:block>
                            <fo:external-graphic  height="auto" width="auto" content-height="1.03in"
                                                    content-width="1.03in" src="url(file:pdf/QR36.png)" />
                        </fo:block> 
                        <fo:block  font-size="7pt" font-family="sans-serif"
                                background-color="white" color="black" text-align-last="center" >                                       
                            <xsl:apply-templates select="floor" />
                            <fo:leader leader-pattern="space" />
                            <fo:external-graphic  height="0.1in" width="0.1in" content-height="0.1in"
                                                    content-width="0.1in" src="url(file:pdf/dot.png)" />
                        </fo:block>                         
                    </fo:table-cell>
                    <fo:table-cell height="1.03in">
                        <fo:block  font-size="7pt" font-family="sans-serif"
                                background-color="white" color="black" text-align="center" >
                            <xsl:apply-templates select="hospital" />
                        </fo:block>
                        <fo:block>
                            <fo:external-graphic  height="auto" width="auto" content-height="1.03in"
                                                    content-width="1.03in" src="url(file:pdf/QR37.png)" />
                        </fo:block> 
                        <fo:block  font-size="7pt" font-family="sans-serif"
                                background-color="white" color="black" text-align-last="center" >                                       
                            <xsl:apply-templates select="floor" />
                            <fo:leader leader-pattern="space" />
                            <fo:external-graphic  height="0.1in" width="0.1in" content-height="0.1in"
                                                    content-width="0.1in" src="url(file:pdf/dot.png)" />
                        </fo:block>     
                    </fo:table-cell>
                    <fo:table-cell height="1.03in">
                        <fo:block  font-size="7pt" font-family="sans-serif"
                                background-color="white" color="black" text-align="center" >
                            <xsl:apply-templates select="hospital" />
                        </fo:block>
                        <fo:block>
                            <fo:external-graphic  height="auto" width="auto" content-height="1.03in"
                                                    content-width="1.03in" src="url(file:pdf/QR38.png)" />
                        </fo:block>     
                        <fo:block  font-size="7pt" font-family="sans-serif"
                                background-color="white" color="black" text-align-last="center" >                                       
                            <xsl:apply-templates select="floor" />
                            <fo:leader leader-pattern="space" />
                            <fo:external-graphic  height="0.1in" width="0.1in" content-height="0.1in"
                                                    content-width="0.1in" src="url(file:pdf/dot.png)" />
                        </fo:block>     
                    </fo:table-cell>
                    <fo:table-cell height="1.03in">
                        <fo:block  font-size="7pt" font-family="sans-serif"
                                background-color="white" color="black" text-align="center" >
                            <xsl:apply-templates select="hospital" />
                        </fo:block>
                        <fo:block>
                            <fo:external-graphic  height="auto" width="auto" content-height="1.03in"
                                                    content-width="1.03in" src="url(file:pdf/QR39.png)" />
                        </fo:block>     
                        <fo:block  font-size="7pt" font-family="sans-serif"
                                background-color="white" color="black" text-align-last="center" >                                       
                            <xsl:apply-templates select="floor" />
                            <fo:leader leader-pattern="space" />
                            <fo:external-graphic  height="0.1in" width="0.1in" content-height="0.1in"
                                                    content-width="0.1in" src="url(file:pdf/dot.png)" />
                        </fo:block>     
                    </fo:table-cell>
                    <fo:table-cell height="1.03in">
                        <fo:block  font-size="7pt" font-family="sans-serif"
                                background-color="white" color="black" text-align="center" >
                            <xsl:apply-templates select="hospital" />
                        </fo:block>
                        <fo:block>
                            <fo:external-graphic  height="auto" width="auto" content-height="1.03in"
                                                    content-width="1.03in" src="url(file:pdf/QR40.png)" />  
                        </fo:block>     
                        <fo:block  font-size="7pt" font-family="sans-serif"
                                background-color="white" color="black" text-align-last="center" >                                       
                            <xsl:apply-templates select="floor" />
                            <fo:leader leader-pattern="space" />
                            <fo:external-graphic  height="0.1in" width="0.1in" content-height="0.1in"
                                                    content-width="0.1in" src="url(file:pdf/dot.png)" />
                        </fo:block>                                                 
                    </fo:table-cell>
                </fo:table-row>             
            </fo:table-body>
        </fo:table>
    </fo:block>
</xsl:template>
</xsl:stylesheet>

0 个答案:

没有答案