jueves, 18 de julio de 2013

Usar funciones de PHP desde Perl fácil



A veces se hace necesario llamar a funciones de php desde un programa en perl. Eso es posible haciendo lo siguiente.

Creamos el archivo test.php con:

<?php

function testPHP($value){
    echo "Pruyeba111 de PHP:" . $value;
}

?>


Luego creamos el archivo perl que usara la función de php y nos queda así:

#!/usr/bin/perl
 
my $res = `php -r '
 
include_once "test.php";
 
testPHP ("\n\nTEEST2\n\n");
'
`;
print "\n\nResultado: $res";


Observar que la comilla en la línea de my $res= es una comilla simple que se usa en perl para ejecutar instrucciones de "afuera" del perl, es similar a escribir por consola o una llamada a la función "system(xxx)" de perl.


lunes, 15 de julio de 2013

Subir archivos al servidor GLPI con FusionInventory manualmente

Para agregar archivos (files) al servidor, cuando creamos un paquete (package), en .
Los archivos deben ser agregados en la carpeta
   <server root>/files/_plugins/fusinvdeploy/upload
Todos los archivos que ponga en esa carpeta los verá en la creación del package de Fusion Inventory yendo por Plugins->Package Management y seleccionando o creando un paquete, cuando hace click en "Add file" en Files to copy on computer.

lunes, 8 de julio de 2013

Imprimir en impresora de etiquetas SATO desde PowerShell

Aquí les dejo un pequeño programa que me habilita la impresión desde PowerShell en impresoras de etiquetas SATO. (parte del código salio de http://social.msdn.microsoft.com/Forums/en-US/94967169-a9ee-45db-9c8a-acd6f173680d/rawprinthelper-for-bold-font)...

El archivo PrinterHelper.ps1 es

clear

$src = @'
using System;
using System.IO;
using System.Runtime.InteropServices;

public class RawPrinterHelper
{
 // Structure and API declarions:
 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
 public class DOCINFOA
 {
  [MarshalAs(UnmanagedType.LPStr)]
  public string pDocName;
  [MarshalAs(UnmanagedType.LPStr)]
  public string pOutputFile;
  [MarshalAs(UnmanagedType.LPStr)]
  public string pDataType;
 }

 #region "Imports"
  [DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);

  [DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  public static extern bool ClosePrinter(IntPtr hPrinter);

  [DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);

  [DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  public static extern bool EndDocPrinter(IntPtr hPrinter);

  [DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  public static extern bool StartPagePrinter(IntPtr hPrinter);

  [DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  public static extern bool EndPagePrinter(IntPtr hPrinter);

  [DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten);
 #endregion


 public  RawPrinterHelper(){}

 // SendBytesToPrinter()
 // When the function is given a printer name and an unmanaged array
 // of bytes, the function sends those bytes to the print queue.
 // Returns true on success, false on failure.
 public  bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount)
 {
  Int32 dwError = 0, dwWritten = 0;
  IntPtr hPrinter = new IntPtr(0);
  DOCINFOA di = new DOCINFOA();
  bool bSuccess = false; // Assume failure unless you specifically succeed.

  di.pDocName = "My document";
  di.pDataType = "RAW";

  try
  {
   // Open the printer.
   if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
   {
    // Start a document.
    if (StartDocPrinter(hPrinter, 1, di))
    {
     // Start a page.
     if (StartPagePrinter(hPrinter))
     {
      // Write your bytes.
      bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
      EndPagePrinter(hPrinter);
     }
     EndDocPrinter(hPrinter);
    }
    ClosePrinter(hPrinter);
   }
  }
  catch (Exception ex)
  {
   throw (ex);
  }
  // If you did not succeed, GetLastError may give more information
  // about why not.
  if (bSuccess == false)
  {
   dwError = Marshal.GetLastWin32Error();
  }
  return bSuccess;
 }

 public  bool SendStringToPrinter(string szPrinterName, string szString)
 {
  IntPtr pBytes;
  Int32 dwCount;
  szString = reemplazarCaracteres(szString);
  // How many characters are in the string?
  dwCount = szString.Length;
  // Assume that the printer is expecting ANSI text, and then convert
  // the string to ANSI text.
  pBytes = Marshal.StringToCoTaskMemAnsi(szString);
  // Send the converted ANSI string to the printer.
  SendBytesToPrinter(szPrinterName, pBytes, dwCount);
  Marshal.FreeCoTaskMem(pBytes);
  return true;
 }

 private string reemplazarCaracteres(string PrintCommand)
 {
  PrintCommand = PrintCommand.Replace("<STX>", ((char)02).ToString());
  PrintCommand = PrintCommand.Replace("<ETX>", ((char)03).ToString());
  PrintCommand = PrintCommand.Replace("<ESC>", ((char)27).ToString());
  return PrintCommand;
 }
}

'@


Add-Type -TypeDefinition $src -Language CSharpVersion3


$PrinterHelper = new-object RawPrinterHelper



 
Su uso es simple, por ejemplo desde otro archivo ps1 hacemos

. .\PrinterHelper.ps1

$PrinterHelper.SendStringToPrinter('\\PROG03\sato cg408', '<STX><ESC>A1002400880<ESC>A<ESC>H0030<ESC>V0030<ESC>BT101030100<ESC>BW03100*555*<ESC>H0030<ESC>V0133<ESC>WB0555<ESC>Q1<ESC>Z<ETX>') 


Con lo que estaríamos imprimiendo una etiqueta con el valor 555 en code39, imprimiendo el número 555 debajo.
Esta misma clase se puede usar para imprimir desde .Net (con c#)

Imprimir etiquetas con impresora SATO GC408TT


La impresión en este modelo de impresora se hace utilizando el lenguaje SBPL. En caso de este modelo de impresora tiene una definición de 8 puntos por milímetro y las medidas se indican en puntos, por lo cual deberemos multiplicar por 8 para tener el tamaño de un mm.

Las impresiones empiezan con <STX> y terminan con <ETX>.
<ESC>A1: para establecer el tamaño de la etiqueta (vendría a ser como el tamaño del papel), el formato es <ESC>A1aaaaabbbb // a = Label Length b = Label Width
Luego en <ESC>H0025<ESC>V0425 indica el desplazamiento en x e y (horizontal y vertical) respectivamente de lo que se va a imprimir.
<ESC>BT101030103 indica el tipo de código
BTabbccddee Bar Codes. Variable Ratio. provides the ability to print a bar code with a
ratio other than those specified through the standard bar code commands
(B, BD, and D).
a = Bar code option:
0: Codabar
1: Code 39
2: Interleaved 2 of 5
5: Industrial 2 of 5
6: Matrix 2 of 5
bb = Narrow space in dots (01-99)
cc = Wide space in dots (01-99)
dd = Narrow bar in dots (01-99)
ee = Wide bar in dots (01-99)
luego de eso sigue:
<ESC>BWaabbb
aa = Expansion factor by which the2width of all bars and spaces will be
increased (01 to 12)
bbb = Bar height by dot (004 to 999 dots)
Place immediately following the <ESC>BT command and preceding data to be
encoded.
EXAMPLE <ESC>A
<ESC>H0050<ESC>V0050<ESC>BT101030103
<ESC>BW04100*1234*
<ESC>Q1
<ESC>Z



Ejemplo:
<STX>
<ESC>A1002400880
<ESC>A
<ESC>H0030<ESC>V0030<ESC>BT101030100<ESC>BW03100*4445*
<ESC>H0030<ESC>V0133<ESC>WB04445
<ESC>Q1
<ESC>Z
<ETX>