Doc to image php

DOCX to JPG PHP API

Convert DOCX files to JPG images using our PHP DOCX to JPG API.

Why PSPDFKit API?

SOC 2 Compliant

Build the workflows you need without worrying about security. We don’t store any document data, and our API endpoints are served through encrypted connections.

Easy Integration

Get up and running in hours, not weeks. Access well-documented APIs and code samples that make integrating with your existing workflows a snap.

One Document, Endless Actions

With access to more than 30 tools, you can process one document in multiple ways by combining API actions. Convert, OCR, rotate, and watermark with one API call.

Simple and Transparent Pricing

Pay only for the number of documents you process. You won’t need to consider file size, datasets being merged, or different API actions being called.

Try It Out

This example will convert your uploaded DOCX file to a JPG image.

Use Your Free API Calls

Sign up to process 100 documents per month for free, or log in to automatically add your API key to sample code.

Add a File

Add a DOCX file named document.docx to your project folder. You can also use our sample file.

Читайте также:  Тег INPUT, атрибут name

The file name is case sensitive. Make sure the file name matches the file name in the sample code. The file name is case sensitive. Make sure the file name matches the file name in the sample code.

Run the Code

Copy the code and run it from the same folder you added the files to. For more information, see our language-specific getting started guides.

View the Results

Open image.jpg in your project folder to view the results.

curl -X POST https://api.pspdfkit.com/build \ -H "Authorization: Bearer your_api_key_here" \ -o image.jpg \ --fail \ -F document=@document.docx \ -F instructions=' < "parts": [ < "file": "document" >], "output": < "type": "image", "format": "jpg", "dpi": 500 >>' 
curl -X POST https://api.pspdfkit.com/build ^ -H "Authorization: Bearer your_api_key_here" ^ -o image.jpg ^ --fail ^ -F document=@document.docx ^ -F instructions="], \"output\": >" 
package com.example.pspdfkit; import java.io.File; import java.io.IOException; import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.StandardCopyOption; import org.json.JSONArray; import org.json.JSONObject; import okhttp3.MediaType; import okhttp3.MultipartBody; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; public final class PspdfkitApiExample < public static void main(final String[] args) throws IOException < final RequestBody body = new MultipartBody.Builder() .setType(MultipartBody.FORM) .addFormDataPart( "document", "document.docx", RequestBody.create( new File("document.docx"), MediaType.parse("application/vnd.openxmlformats-officedocument.wordprocessingml.document") ) ) .addFormDataPart( "instructions", new JSONObject() .put("parts", new JSONArray() .put(new JSONObject() .put("file", "document") ) ) .put("output", new JSONObject() .put("type", "image") .put("format", "jpg") .put("dpi", 500) ).toString() ) .build(); final Request request = new Request.Builder() .url("https://api.pspdfkit.com/build") .method("POST", body) .addHeader("Authorization", "Bearer your_api_key_here") .build(); final OkHttpClient client = new OkHttpClient() .newBuilder() .build(); final Response response = client.newCall(request).execute(); if (response.isSuccessful()) < Files.copy( response.body().byteStream(), FileSystems.getDefault().getPath("image.jpg"), StandardCopyOption.REPLACE_EXISTING ); >else < // Handle the error throw new IOException(response.body().string()); >> > 
using System; using System.IO; using System.Net; using RestSharp; namespace PspdfkitApiDemo < class Program < static void Main(string[] args) < var client = new RestClient("https://api.pspdfkit.com/build"); var request = new RestRequest(Method.POST) .AddHeader("Authorization", "Bearer your_api_key_here") .AddFile("document", "document.docx") .AddParameter("instructions", new JsonObject < ["parts"] = new JsonArray < new JsonObject < ["file"] = "document" >>, ["output"] = new JsonObject < ["type"] = "image", ["format"] = "jpg", ["dpi"] = 500 >>.ToString()); request.AdvancedResponseWriter = (responseStream, response) => < if (response.StatusCode == HttpStatusCode.OK) < using (responseStream) < using var outputFileWriter = File.OpenWrite("image.jpg"); responseStream.CopyTo(outputFileWriter); >> else < var responseStreamReader = new StreamReader(responseStream); Console.Write(responseStreamReader.ReadToEnd()); >>; client.Execute(request); > > > 
// This code requires Node.js. Do not run this code directly in a web browser. const axios = require('axios') const FormData = require('form-data') const fs = require('fs') const formData = new FormData() formData.append('instructions', JSON.stringify( < parts: [ < file: "document" >], output: < type: "image", format: "jpg", dpi: 500 >>)) formData.append('document', fs.createReadStream('document.docx')) ;(async () => < try < const response = await axios.post('https://api.pspdfkit.com/build', formData, < headers: formData.getHeaders(< 'Authorization': 'Bearer your_api_key_here' >), responseType: "stream" >) response.data.pipe(fs.createWriteStream("image.jpg")) > catch (e) < const errorString = await streamToString(e.response.data) console.log(errorString) >>)() function streamToString(stream) < const chunks = [] return new Promise((resolve, reject) => < stream.on("data", (chunk) =>chunks.push(Buffer.from(chunk))) stream.on("error", (err) => reject(err)) stream.on("end", () => resolve(Buffer.concat(chunks).toString("utf8"))) >) > 
import requests import json instructions = < 'parts': [ < 'file': 'document' >], 'output': < 'type': 'image', 'format': 'jpg', 'dpi': 500 >> response = requests.request( 'POST', 'https://api.pspdfkit.com/build', headers = < 'Authorization': 'Bearer your_api_key_here' >, files = < 'document': open('document.docx', 'rb') >, data = < 'instructions': json.dumps(instructions) >, stream = True ) if response.ok: with open('image.jpg', 'wb') as fd: for chunk in response.iter_content(chunk_size=8096): fd.write(chunk) else: print(response.text) exit() 
 ], "output": < "type": "image", "format": "jpg", "dpi": 500 >>'; curl_setopt_array($curl, array( CURLOPT_URL => 'https://api.pspdfkit.com/build', CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_POSTFIELDS => array( 'instructions' => $instructions, 'document' => new CURLFILE('document.docx') ), CURLOPT_HTTPHEADER => array( 'Authorization: Bearer your_api_key_here' ), CURLOPT_FILE => $FileHandle, )); $response = curl_exec($curl); curl_close($curl); fclose($FileHandle); 

Using Postman? Download our official collection and start using the API with a single click. Read more →

Your API Key

Get access to your API key when you create an account. Once your account has been created, you’ll be able to process 100 documents per month for free using any of our API tools.

Your API key has automatically been inserted into the API example code. Run the sample code in your terminal to execute the API call.

Источник

PHP — DOCX to JPG

Zamzar offers a simple file conversion API to convert files from your applications with support for 100’s of formats. Below we have listed an example to convert a DOCX file to JPG using PHP. We also support a variety of other programming languages.

If you have any questions check out our comprehensive FAQ which contains further information on how to use the API.

PHP DOCX to JPG PHP DOCX to JPG PHP DOCX to JPG

PHP Code Sample

Our example code assumes that you’re using PHP 5.3 or newer.

To convert your first file with the Zamzar API, send an HTTP request to POST https://sandbox.zamzar.com/v1/jobs containing your source file, and the your desired target format. If the source file is on the web or in S3, send us the URL: the source file doesn’t need to hit your servers.

 $sourceFile, "target_format" => $targetFormat ); $ch = curl_init(); // Init curl curl_setopt($ch, CURLOPT_URL, $endpoint); // API endpoint curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return response as a string curl_setopt($ch, CURLOPT_USERPWD, $apiKey . ":"); // Set the API key as the basic auth username $body = curl_exec($ch); curl_close($ch); $response = json_decode($body, true); echo "Response:\n---------\n"; print_r($response); 

Your source file is now being converted. Send an HTTP request to GET https://sandbox.zamzar.com/v1/jobs/$jobId to check its progress. The response will also give you details about your converted file.

Once the status of your job is successful , your converted file is ready to download. Send an HTTP request to GET https://sandbox.zamzar.com/v1/file/$fileId/content to download it. We store your files for a day by default, and for longer on our paid plans.

Why use Zamzar?

Hosting

Don’t worry about hosting and using your own servers we do this for you.

S3 Integration

Automatically import and export to S3 with 2 lines of code.

Simple Pricing

Fixed price monthly accounts which come bundled with conversion credits.

Great Support

Our support team is staffed by software developers who will help to fix your problem.

Conversion Experts

Using DOCX with PHP

You could consider using an open source library (licensed under LGPL version 3) – PHPWord. This is a widely used tool online with fairly detailed documentation. In terms of manipulation of the documents it is a good solution, but does have some limitations when converting into other formats. It is certainly a tool that is worth trialling, as is quite widely used and updated regularly.

Another open source option is to use LibreOffice Writer. It offers both a graphical and a command line interface for manipulation and conversion. It is unfortunately fairly limited in its support for PHP, but you could consider utilising both LibreOffice and Unoconv. There is a good sample article on how you could combine LibreOffice and Unoconv using a PHP wrapper. There maybe some accuracy issues if you add complex tables and images, but is worth testing on your files to see if it suits your needs.

If you need commercial software that renders as accurately as the native Microsoft Office tooling you could consider using the Zamzar API. With a dedicated support team, code examples in many of the major languages including PHP, simple low cost conversion credits and support for direct import and export to S3, it may cover most of the use cases you require. Feel free to reach out to our support team with any questions or dip into the getting started guide in our docs.

Resources:
  • Use a PHP wrapper to convert from DOCX to PDF using Unoconv and LibreOffice
  • PHPWord a PHP library used to manipulate Microsoft Word Documents
Related StackOverflow Questions:

Using JPG with PHP

You have a number of options when looking to convert JPG files using PHP and each of them have various pros and cons. You could consider using the GD PHP library which has quite extensive support for image processing and has a large number of online resources which you can use as a great reference guide when writing your code.

Another option is to use an open source tool such as ImageMagick, which is known as the «Swiss Army Knife» of image processing. It has a large number of configurable options and is fairly easily configurable through a handy object-oriented interface — see these examples. It also has extensive documentation and there is a wide body of online support in forums should you hit technical issues.

If you’d prefer to use commercial software with dedicated support from developers and a high quality rendering engine you could consider using the Zamzar API. With code examples in many of the major languages including PHP, simple low cost conversion credits and support for direct import and export to S3, it may cover most of the use cases you have. Feel free to reach out to our support team with any questions or dip into the getting started guide in our docs.

Resources:
Related StackOverflow Questions:

Источник

Оцените статью