lineofflight / peddler

Amazon Selling Partner API (SP-API) in Ruby
MIT License
307 stars 130 forks source link

MWS FBA get_package_labels (GetPackageLabels) as PDF file #118

Closed blairanderson closed 5 years ago

blairanderson commented 6 years ago

https://docs.developer.amazonservices.com/en_US/fba_inbound/FBAInbound_ExtractingPdfDocumentData.html

get_content = client.fulfillment_inbound_shipment.get_package_labels(
  fba_shipment_id,
  page_type = "PackageLabel_Letter_2"
).parse

label_tempfile = Tempfile.new([fba_shipment_id, '.zip'])

File.open(label_tempfile, "wb") do |f|
  f.write(Base64.decode64(get_content.dig("TransportDocument", "PdfDocument")))
end

file_path = Rails.root.join('tmp', "#{fba_shipment_id}.pdf")

Zip::File.open(label_tempfile) do |zipfile|
  zipfile.each do |file|
    if file.name == "PackageLabels.pdf"
      File.delete(file_path) if File.exists?(file_path) # overwrite file if we call multiple times
      file.extract(file_path) # saves data to file_path
      labels = File.open(file_path) # this is how i'm persisting the files to my database/s3
      shipment.labels = labels # this is how i'm persisting the files to my database/s3
      shipment.save! # this is how i'm persisting the files to my database/s3
      labels.close # make sure file is closed
    end
  end
end
label_tempfile.close!

This lib parses CSV files from other parts of the API and might be a nice addition for the PDF as well.

not sure if/how/where you would accept this.

blairanderson commented 6 years ago

FYI @hakanensari thank you for this GEM. Amazing 👍

hakanensari commented 6 years ago

@blairanderson I’m not sure if there any other operations elsewhere in the API that serve PDFs. This could certainly be something useful.

I’m off the grid now but will look into this when I return next month.

blairanderson commented 6 years ago

Yup. The documentation on page says:

The GetPackageLabels, GetUniquePackageLabels, GetPalletLabels, and GetBillOfLading operations return PDF document data for printing shipping labels and carrier labels for an inbound shipment. Amazon compresses PDF document data before returning it as a Base64-encoded string. To obtain the actual PDF document, you must decode the Base64-encoded string, save it as a binary file with a “.zip” extension, and then extract the PDF file from the ZIP file. These operations also return a Base64-encoded MD5 hash to validate the document data.

I figure that people using this lib would want to do those other requests as well.

stale[bot] commented 5 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

blairanderson commented 5 years ago

close

On Oct 19, 2018, at 6:08 PM, stale[bot] notifications@github.com wrote:

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

hakanensari commented 5 years ago

@blairanderson sorry for the late response, and thanks for taking the time to post and explain.

I did some digging around and found that the Merchant Fulfilment API is also embedding labels, but in a slightly different way—they're gzipped instead of zipped. I'm not sure if it matters, but the keys are also named differently.

Long story short, it seems there are at least two slightly different paths.

The current parser simply delegates to MultiXml.parse, which doesn't typecast values. This seems what we want to do here—detect and unpack values that contain binary data, perhaps saving the output to a TempFile.

To he honest, I'm not sure either how to go about this. Perhaps a first good step could be to simply add a section on this to the README instead of attempting to shoehorn some complex logic into the main library 🤔

blairanderson commented 5 years ago

Totally cool.

I’m actually having trouble with carton labels for multi-sku shipments.

I do think it would be better just adding to the docs instead of implementing helpers because they might not get much direct usage.

Thanks again for help

stale[bot] commented 5 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.