Hoy he llevado al entorno de pruebas un script que se encargaba de descargar un PDF de la AEAT a partir de una referencia y como no, al ponerlo sobre un windows me he econtrado con el tipico problema de poner algo que funciona sobre el Engendro de Redmond: No funciona.Mejor dicho, todo funcionaba, a diferencia que el PDF que se descargaba no se podia abrir.
He decidido comparar el mismo PDF ejecutando el script sobre MacOS,Linux y Windows cuando me he dado cuenta que Windows añadia un bonito 0x0D (retorno de carro) antes de cada 0x0A (fin de linea). La solucion al problema pasaba por intentar cambiar los CR/LF por LF con una funcion como esta:
def removeCR(line)
line = line[0..-3]+line[-1..-1] if line[-2]==0xD
return line
end
pero realmente, los CR/LF los metia la funcion File::print o puts...¿como solventarlo?
Al final la solucion pasa por abrir el fichero en modo w+b
file=File.new("#{filename}.pdf","wb")
Podemos leer de la clase IO la descripcion de cada uno de los modos
Mode | Meaning
-----+--------------------------------------------------------
"r" | Read-only, starts at beginning of file (default mode).
-----+--------------------------------------------------------
"r+" | Read-write, starts at beginning of file.
-----+--------------------------------------------------------
"w" | Write-only, truncates existing file
| to zero length or creates a new file for writing.
-----+--------------------------------------------------------
"w+" | Read-write, truncates existing file to zero length
| or creates a new file for reading and writing.
-----+--------------------------------------------------------
"a" | Write-only, starts at end of file if file exists,
| otherwise creates a new file for writing.
-----+--------------------------------------------------------
"a+" | Read-write, starts at end of file if file exists,
| otherwise creates a new file for reading and
| writing.
-----+--------------------------------------------------------
"b" | (DOS/Windows only) Binary file mode (may appear with
| any of the key letters listed above).