问题
So I'm working my way through the UPS API 'create shipping label' process...I'm at the final stage, where having validated everything, UPS return the actual shipping label to me in GIF format, from their example document, here's what is returned via XML (I've edited out all the other preceding XML bits)..
<GraphicImage>FSDJHSDJHJ3487EHNE9U8DY9VHRFV89SDFHFSDJHFSDIDFH
SJKDFSJKDFSJIU9GFIUGJIFDUJG9UKGLDJFDKJDGKJDFKGDJLDFKSJGKDFJDKGFDG9E0ER
IJGE39IWURE9U9ER0UW9R0UR9WEGU9URE9WGUW90U90GRUG90GERUG9REUGWERGJIO
JGIODFGUIOFDUGIOFUIGRUE090U9TERUT90RUT9EU90ERUT9ERU9EUER9TUT9R0UTE90R
U9TERU90RTEU9SDKHGJHGDFU</GraphicImage>
<HTMLImage>SKJJKLHGIGKHGKJHGFJGFJHDFJGHDDJFHFDJHFJHFJKDHJK
FDHJFJDFHDFJHJDFHGJDHGDFSHJKFSDHSDFJHFJSDHJKDFHFJKSHDSKJHGFDJSJDFSKSK
JJKLHGIGKHGKJHGFJGFJHDFJGHDDJFHFDJHFJHFJKDHJKFDHJFJDFHDFJHJDFHGJDHGDF
SHJKFSDHSDFJHFJSDHJKDFHFJKSHDSKJHGFDJSJDFSKHGJKDS</HTMLImage>
..what am I meant to do with either that GIF data or that HTMLIMage data? For example I piped the GIF data into a gif file - but the GIF file won't open - is there something else I need to do?
回答1:
Most shipping API's return labels as Base 64 encoded strings (in JSON or XML). You'll need to convert the content from base 64 to an bite array, and save it.
The way you do the Base64 conversion varies by development platform, but there are plenty of examples. Here's one I just found.
http://processrhetoric.blogspot.com/2013/01/getting-ups-shipping-label-via-their-api.html
If you use the Google Chrome browser, you can preview the image by building a string that starts with
data:image/gif;base64,
Then add the image content (though your example content doesn't seem to work).
data:image/gif;base64,FSDJHSDJHJ3487EHNE9U8DY9VHRFV89SDFHFSDJHFSDIDFH
SJKDFSJKDFSJIU9GFIUGJIFDUJG9UKGLDJFDKJDGKJDFKGDJLDFKSJGKDFJDKGFDG9E0ER
IJGE39IWURE9U9ER0UW9R0UR9WEGU9URE9WGUW90U90GRUG90GERUG9REUGWERGJIO
JGIODFGUIOFDUGIOFUIGRUE090U9TERUT90RUT9EU90ERUT9ERU9EUER9TUT9R0UTE90R
U9TERU90RTEU9SDKHGJHGDFU
And put the results in the browser address bar, and the image will appear.
In JavaScript / VBScript, use ADODB to save the bytes that you create from base64 to disk as a GIF.
var stream = new ActiveXObject("ADODB.Stream");
try {
if (objFSO.FileExists(strFileName)) {
objFSO.DeleteFile(strFileName);
}
stream.Type = 2; // text
stream.Charset = "ISO-8859-1";
stream.Open();
stream.WriteText(bytes);
stream.SaveToFile( strFileName, 2);
} catch (err){
return;
} finally {
if (stream.State != 0) {
stream.Close();
}
}
回答2:
For this we need two gems:
gem "wicked_pdf"
gem 'wkhtmltopdf-binary'
ups_gif_image = base64 GIF image # This is the
base64 graphicimage in the form of GIF that is returned from UPS
shipment request API
ups_base64_gif_image ="data:image/gif;base64,"+ups_gif_image
pdf_data = WickedPdf.new.pdf_from_string("<img src='"+ups_base64_gif_image+"'/>",{:orientation => 'Landscape'})
File.open('label.pdf','wb') do |pdf|
pdf.write pdf_data
pdf.close
end
来源:https://stackoverflow.com/questions/50986453/ups-api-printing-the-returned-shipping-label-gif-data