You can practice more with this until you'll find exactly what you are searching for.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$_order = $objectManager->create('\Magento\Sales\Model\Order');
$order = $_order->load(1);
if (!$order->canShip()) {
throw new \Magento\Framework\Exception\LocalizedException(
__('You can\'t create an shipment.')
);
}
$packageParams = array();
$packageParams['width'] = 100;
$packageParams['length'] = 100;
$packageParams['height'] = 100;
$packageParams['weight'] = 1;
$convertOrder = $objectManager->create('\Magento\Sales\Model\Convert\Order');
$shipment = $convertOrder->toShipment($order);
$package = array();
$packaging = array(
'items' => array()
);
$subtotal = 0;
foreach ($order->getAllItems() AS $orderItem) {
if (! $orderItem->getQtyToShip() || $orderItem->getIsVirtual()) {
continue;
}
$qtyShipped = $orderItem->getQtyToShip();
$shipmentItem = $convertOrder->itemToShipmentItem($orderItem)->setQty($qtyShipped);
$packaging['items'][$shipmentItem->getOrderItemId()] = array(
'qty' => $shipmentItem->getQty(),
'custom_value' => $shipmentItem->getPrice(),
'price' => $shipmentItem->getPrice(),
'name' => $shipmentItem->getName(),
'weight' => $shipmentItem->getWeight(),
'product_id' => $shipmentItem->getProductId(),
'order_item_id' => $shipmentItem->getOrderItemId()
);
$subtotal += $shipmentItem->getRowTotal();
$shipment->addItem($shipmentItem);
}
if(empty($packaging['items']))
{
throw new \Magento\Framework\Exception\LocalizedException(__('No items to ship'));
}
$packaging['params'] = array(
'container' => '',
'weight' => $packageParams['weight'],
'custom_value' => $subtotal,
'length' => $packageParams['length'],
'width' => $packageParams['width'],
'height' => $packageParams['height'],
'weight_units' => 'KILOGRAM',
'dimension_units' => 'CENTIMETER',
'content_type' => '',
'content_type_other' => ''
);
$package[] = $packaging;
$shipment->setPackages($package);
$shipment->register();
$shipment->getOrder()->setIsInProcess(true);
try {
$shipment->save();
$shipment->getOrder()->save();
$notify = $objectManager->create('Magento\Shipping\Model\ShipmentNotifier');
$notify->notify($shipment);
$shipment->save();
} catch (\Exception $e) {
throw new \Magento\Framework\Exception\LocalizedException(
__($e->getMessage())
);
}