Skip to content

I have this error: Error generating report (NameError): Error fill report: Erro fill internal: net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression for source text: $P{timbrado} #170

@arielmedinaa

Description

@arielmedinaa

this is my views (in my django project)

from django.http import HttpResponse
import os
from pyreportjasper import PyReportJasper

def generar_pdf(request):
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    RESOURCES_DIR = os.path.join(BASE_DIR, 'Reports', '80010184-7')
    input_file = os.path.join(RESOURCES_DIR, 'SimpleReport.jrxml')
    output_file = os.path.join(RESOURCES_DIR, 'output')

    # Forzar el parámetro timbrado manualmente
    parameters = {
        'timbrado': 16313356  # Un valor de prueba para timbrado
    }

    # Configurar el classpath
    java_classpath = ":".join([
        '/mnt/data/bcprov-jdk15on-1.68.jar',
        '/mnt/data/commons-collections4-4.2.jar',
        '/mnt/data/commons-digester-2.1.jar',
        '/mnt/data/commons-lang-2.6.jar',
        '/mnt/data/commons-logging-1.1.1.jar',
        '/mnt/data/ecj-3.21.0.jar',
        '/mnt/data/groovy-all-2.4.15.jar'
    ])
    os.environ["CLASSPATH"] = java_classpath

    # Verificar todos los parámetros antes de configurar PyReportJasper
    for key, value in parameters.items():
        print(f"Parameter: {key}, Value: {value}, Type: {type(value)}")

    # Debugging: Print classpath
    print(f"CLASSPATH: {os.environ['CLASSPATH']}")

    pyreportjasper = PyReportJasper()

    try:
        pyreportjasper.config(
            input_file=input_file,
            output_file=output_file,
            output_formats=["pdf"],
            parameters=parameters
        )
        print("PyReportJasper configured successfully.")
    except Exception as e:
        print("Error configuring PyReportJasper:", e)
        return HttpResponse("Error configuring PyReportJasper: {}".format(e), status=500)

    # Generar el reporte
    try:
        pyreportjasper.process_report()
        print("Report generated successfully.")
    except NameError as e:
        print("Error generating report (NameError):", e)
        return HttpResponse("Error generating report (NameError): {}".format(e), status=500)
    except Exception as e:
        print("Error generating report:", e)
        return HttpResponse("Error generating report: {}".format(e), status=500)

    # Leer y devolver el archivo PDF generado
    try:
        pdf_file_path = output_file + '.pdf'
        with open(pdf_file_path, 'rb') as pdf_file:
            response = HttpResponse(pdf_file.read(), content_type='application/pdf')
            response['Content-Disposition'] = 'inline; filename="output.pdf"'
            print("PDF file generated and ready to be sent.")
            return response
    except Exception as e:
        print("Error reading PDF file:", e)
        return HttpResponse("Error reading PDF file: {}".format(e), status=500)

I am taking the parameters from an xml, this is the project folder where I am extracting the data.

import xml.etree.ElementTree as ET
from datetime import datetime

def remove_namespace(tree):
    for elem in tree.iter():
        if '}' in elem.tag:
            elem.tag = elem.tag.split('}', 1)[1]  # Remove namespace
        for name, value in list(elem.attrib.items()):
            if '}' in name:
                new_name = name.split('}', 1)[1]
                elem.attrib[new_name] = value
                del elem.attrib[name]

def get_text(element, xpath, default=''):
    result = element.find(xpath)
    return result.text if result is not None else default

#Solo sirve para el puto cdc xd
def get_attribute(element, xpath, attribute, default=''):
    result = element.find(xpath)
    return result.attrib.get(attribute, default) if result is not None else default

def safe_int(value, default=0):
    try:
        return int(value)
    except (ValueError, TypeError):
        return default

def parse_date(date_str):
    try:
        date = datetime.strptime(date_str, "%Y-%m-%dT%H:%M:%S")
        return date.strftime('%Y-%m-%d')
    except (ValueError, TypeError):
        return '1970-01-01'

def extraer_xml(file_path):
    tree = ET.parse(file_path)
    remove_namespace(tree)
    root = tree.getroot()

    data = {
        'cdc': get_attribute(root, './/DE', 'Id'),
        'rucEmi': get_text(root, './/dRucEm'),
        'timbrado': safe_int(get_text(root, './/dNumTim', 0)),
        'dDirEmi': get_text(root, './/dDirEmi', ''),
        'dTelEmi': get_text(root, './/dTelEmi', ''),
        'dEmailE': get_text(root, './/dEmailE', ''),
        'dDesActEco': get_text(root, './/dDesActEco', ''),
        'empresa': get_text(root, './/dNomEmi', ''),
        'fechaDE': parse_date(get_text(root, './/dFeEmiDE')),
        'nroFac': get_text(root, './/dNumDoc'),
        'tipDE': get_text(root, './/dDesTiDE').upper(),
        'dexp': get_text(root, './/dPunExp'),
        'dsuc': get_text(root, './/dEst'),
        'moneda': get_text(root, './/dDesMoneOpe'),
        'cambio': get_text(root, './/dTiCam', '0'),
        'condicion': get_text(root, './/dDCondOpe'),
        'plazo': get_text(root, './/dPlazoCre', ''),
        'cuotas': get_text(root, './/dCuotas', ''),
        'rucCli': get_text(root, './/dCodCliente', ''),
        'cliNom': get_text(root, './/dNomRec'),
        'dire': get_text(root, './/dDirRec', '-'),
        'tel': get_text(root, './/dTelRec', ''),
        'mail': get_text(root, './/dEmailRec', ''),
        'tipTransac': get_text(root, './/dDesTipTra'),
        'subtotal': get_text(root, './/dSub10', '0'),
        'subtotalMe': get_text(root, './/dSub10', '0'),
        'totalOp': get_text(root, './/dTotOpe', '0'),
        'totalOpMe': get_text(root, './/dTotOpe', '0'),
        'total': get_text(root, './/dTotGralOpe', '0'),
        'totalMe': get_text(root, './/dTotGralOpe', '0'),
        'descuento': get_text(root, './/dDescTotal', '0'),
        'descuentoMe': get_text(root, './/dDescTotal', '0'),
        'urlLogo': 'G:\Server-report\Reports\80010184-7\logo\logo.png',
    }
    return data

I extract all the data from an xml, except for some data

<DE Id="01800334973001001000308012024052811336721411"> <dDVId>1</dDVId> <dFecFirma>2024-05-28T09:18:17</dFecFirma> <dSisFact>1</dSisFact> <gOpeDE> <iTipEmi>1</iTipEmi> <dDesTipEmi>Normal</dDesTipEmi> <dCodSeg>133672141</dCodSeg> </gOpeDE> <gTimb> <iTiDE>1</iTiDE> <dDesTiDE>Factura electrónica</dDesTiDE> <dNumTim>17109678</dNumTim> <dEst>001</dEst> <dPunExp>001</dPunExp> <dNumDoc>0003080</dNumDoc> <dFeIniT>2024-03-21</dFeIniT> </gTimb> <gDatGralOpe> <dFeEmiDE>2024-05-28T09:16:29</dFeEmiDE> <gOpeCom> <iTipTra>1</iTipTra> <dDesTipTra>Venta de mercadería</dDesTipTra> <iTImp>5</iTImp> <dDesTImp>IVA - Renta</dDesTImp> <cMoneOpe>PYG</cMoneOpe> <dDesMoneOpe>Guarani</dDesMoneOpe> <iCondAnt>1</iCondAnt> <dDesCondAnt>Anticipo Global</dDesCondAnt> </gOpeCom> <gEmis> <dRucEm>80033497</dRucEm> <dDVEmi>3</dDVEmi> <iTipCont>1</iTipCont> <cTipReg>8</cTipReg> <dNomEmi>INOXPAR S.R.L.</dNomEmi> <dDirEmi>DEFENSORES DEL CHACO</dDirEmi> <dNumCas>467</dNumCas> <cDepEmi>12</cDepEmi> <dDesDepEmi>CENTRAL</dDesDepEmi> <cDisEmi>165</cDisEmi> <dDesDisEmi>VILLA ELISA</dDesDisEmi> <cCiuEmi>6049</cCiuEmi> <dDesCiuEmi>VILLA ELISA</dDesCiuEmi> <dTelEmi>021944333</dTelEmi> <dEmailE>facturacion.inoxparsrl@gmail.com</dEmailE> <gActEco> <cActEco>46699</cActEco> <dDesActEco>COMERCIO AL POR MAYOR DE OTROS PRODUCTOS N.C.P.</dDesActEco> </gActEco> <gRespDE> <iTipIDRespDE>1</iTipIDRespDE> <dDTipIDRespDE>Cédula paraguaya</dDTipIDRespDE> <dNumIDRespDE>571977</dNumIDRespDE> <dNomRespDE>ALIPIO R. IBARRA IBARRA</dNomRespDE> <dCarRespDE>REPRESENTANTE LEGAL</dCarRespDE> </gRespDE> </gEmis> <gDatRec> <iNatRec>1</iNatRec> <iTiOpe>2</iTiOpe> <cPaisRec>PRY</cPaisRec> <dDesPaisRe>Paraguay</dDesPaisRe> <iTiContRec>2</iTiContRec> <dRucRec>80007165</dRucRec> <dDVRec>4</dDVRec> <dNomRec>TECNIMET S.A</dNomRec> <dDirRec>Calle Coronel Martinez Luque Paraguay. Km 73</dDirRec> <dNumCasRec>0</dNumCasRec> <cDepRec>1</cDepRec> <dDesDepRec>CAPITAL</dDesDepRec> <cDisRec>1</cDisRec> <dDesDisRec>ASUNCION (DISTRITO)</dDesDisRec> <cCiuRec>1</cCiuRec> <dDesCiuRec>ASUNCION (DISTRITO)</dDesCiuRec> <dEmailRec>administracion@tecnimet.com</dEmailRec> <dCodCliente>80007165-4</dCodCliente> </gDatRec> </gDatGralOpe> <gDtipDE> <gCamFE> <iIndPres>1</iIndPres> <dDesIndPres>Operación presencial</dDesIndPres> <dFecEmNR>2024-05-28</dFecEmNR> </gCamFE> <gCamCond> <iCondOpe>2</iCondOpe> <dDCondOpe>Crédito</dDCondOpe> <gPaConEIni> <iTiPago>1</iTiPago> <dDesTiPag>Efectivo</dDesTiPag> <dMonTiPag>0</dMonTiPag> <cMoneTiPag>PYG</cMoneTiPag> <dDMoneTiPag>Guarani</dDMoneTiPag> </gPaConEIni> <gPagCred> <iCondCred>1</iCondCred> <dDCondCred>Plazo</dDCondCred> <dPlazoCre>30 días</dPlazoCre> <dMonEnt>0</dMonEnt> </gPagCred> </gCamCond> <gCamItem> <dCodInt>070-00031</dCodInt> <dDesProSer>CHAPA ANTID. DE ALUM. 3003 H26 DE 1,50MM 1.250 X 3.000</dDesProSer> <cUniMed>77</cUniMed> <dDesUniMed>UNI</dDesUniMed> <dCantProSer>10.0000</dCantProSer> <cPaisOrig>PRY</cPaisOrig> <dDesPaisOrig>Paraguay</dDesPaisOrig> <dCanQuiMer>0</dCanQuiMer> <dPorQuiMer>0</dPorQuiMer> <gValorItem> <dPUniProSer>880000.00000000</dPUniProSer> <dTotBruOpeItem>8800000.00000000</dTotBruOpeItem> <gValorRestaItem> <dDescItem>0.00000000</dDescItem> <dPorcDesIt>0.00000000</dPorcDesIt> <dDescGloItem>0</dDescGloItem> <dAntPreUniIt>0</dAntPreUniIt> <dAntGloPreUniIt>0</dAntGloPreUniIt> <dTotOpeItem>8800000.00000000</dTotOpeItem> <dTotOpeGs>0</dTotOpeGs> </gValorRestaItem> </gValorItem> <gCamIVA> <iAfecIVA>1</iAfecIVA> <dDesAfecIVA>Gravado IVA</dDesAfecIVA> <dPropIVA>100</dPropIVA> <dTasaIVA>10</dTasaIVA> <dBasGravIVA>8000000.00000000</dBasGravIVA> <dLiqIVAItem>800000.00000000</dLiqIVAItem> <dBasExe>0</dBasExe> </gCamIVA> <gRasMerc/> </gCamItem> <gCamEsp> <gGrupEner> <dNroMed>0</dNroMed> <dActiv>0</dActiv> <dCateg>0</dCateg> <dLecAnt>0</dLecAnt> <dLecAct>0</dLecAct> <dConKwh>0</dConKwh> </gGrupEner> </gCamEsp> <gTransp> <iModTrans>1</iModTrans> <dDesModTrans>Terrestre</dDesModTrans> <iRespFlete>1</iRespFlete> </gTransp> </gDtipDE> <gTotSub> <dSubExe>0.00000000</dSubExe> <dSubExo>0</dSubExo> <dSub5>0.00000000</dSub5> <dSub10>8800000.00000000</dSub10> <dTotOpe>8800000.00000000</dTotOpe> <dTotDesc>0.00000000</dTotDesc> <dTotDescGlotem>0</dTotDescGlotem> <dTotAntItem>0</dTotAntItem> <dTotAnt>0</dTotAnt> <dPorcDescTotal>0</dPorcDescTotal> <dDescTotal>0.00000000</dDescTotal> <dAnticipo>0</dAnticipo> <dRedon>0</dRedon> <dComi>0</dComi> <dTotGralOpe>8800000.00000000</dTotGralOpe> <dIVA5>0.00000000</dIVA5> <dIVA10>800000.00000000</dIVA10> <dLiqTotIVA5>0</dLiqTotIVA5> <dLiqTotIVA10>0</dLiqTotIVA10> <dIVAComi>0</dIVAComi> <dTotIVA>800000.00000000</dTotIVA> <dBaseGrav5>0.00000000</dBaseGrav5> <dBaseGrav10>8000000.00000000</dBaseGrav10> <dTBasGraIVA>8000000.00000000</dTBasGraIVA> </gTotSub> <gCamGen> <gCamCarg/> </gCamGen> </DE>
Please help me to fix this shit

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions