Я работаю над Java NetBeans application
, где пользователь может создать лист из 40 QR-кодов в файле PDF, для этого я использую Apache FOP
, Для разработки / форматирования QR-кода PDF я использую xlsx
файл, для динамических данных xml
файл.
Проблема заключается в том, что после печати PDF на листе А4 он отличается от других систем.
Кто-нибудь может сказать, в чем может быть проблема, поскольку эти штрих-коды должны быть напечатаны на листах с наклейками, так как они будут использоваться в качестве наклеек? Выравнивание должно быть постоянным во всех системах.
Буду признателен за любую помощь и любую дополнительную информацию, которую я предоставлю.
Способ создания 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>