Quick Start for Go
Go offers libraries for programmatic PDF creation and for browser automation, but they often require additional dependencies or complex integration. PDFBolt's PDF generation API provides an efficient solution for generating PDFs from HTML, URLs, and dynamic templates that works seamlessly with Go.
1. Get Your API Key
- After signing up, locate your API Key in the Admin Dashboard under the API Keys section.
- This key is essential for authorizing your requests to the PDFBolt API. Remember to keep it secure.
2. Make Your First Request
Get started with these examples to send requests to the PDFBolt API and generate PDFs instantly.
Choose your preferred endpoint and source combination.
➡️ Endpoints:
- Direct
- Sync
- Async
The Direct endpoint provides immediate PDF generation and returns the raw PDF file in the response.
➡️ Sources:
- URL
- HTML
- Template
Convert any webpage into a PDF:
package main
import (
"bytes"
"fmt"
"io"
"net/http"
"os"
)
func main() {
jsonBody := `{
"url": "https://example.com",
"format": "A4",
"printBackground": true
}`
req, _ := http.NewRequest("POST", "https://api.pdfbolt.com/v1/direct", bytes.NewBufferString(jsonBody))
req.Header.Add("API-KEY", "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")
req.Header.Add("Content-Type", "application/json")
resp, _ := http.DefaultClient.Do(req)
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
fmt.Printf("HTTP %d\n", resp.StatusCode)
body, _ := io.ReadAll(resp.Body)
fmt.Printf("Error Message: %s\n", string(body))
return
}
file, _ := os.Create("webpage.pdf")
defer file.Close()
io.Copy(file, resp.Body)
fmt.Println("PDF generated successfully")
}
Convert HTML content directly into a PDF:
package main
import (
"bytes"
"encoding/base64"
"fmt"
"io"
"net/http"
"os"
)
func main() {
htmlContent := "<html><body><h1>Hello!</h1><p>This is a sample PDF.</p></body></html>"
base64Html := base64.StdEncoding.EncodeToString([]byte(htmlContent))
jsonBody := fmt.Sprintf(`{
"html": "%s",
"margin": {
"top": "30px",
"left": "30px"
}
}`, base64Html)
req, _ := http.NewRequest("POST", "https://api.pdfbolt.com/v1/direct", bytes.NewBufferString(jsonBody))
req.Header.Add("API-KEY", "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")
req.Header.Add("Content-Type", "application/json")
resp, _ := http.DefaultClient.Do(req)
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
fmt.Printf("HTTP %d\n", resp.StatusCode)
body, _ := io.ReadAll(resp.Body)
fmt.Printf("Error Message: %s\n", string(body))
return
}
file, _ := os.Create("document.pdf")
defer file.Close()
io.Copy(file, resp.Body)
fmt.Println("PDF generated successfully")
}
Use reusable templates for dynamic PDF generation:
package main
import (
"bytes"
"fmt"
"io"
"net/http"
"os"
)
func main() {
jsonBody := `{
"templateId": "your-template-id",
"templateData": {
"client_name": "John Doe",
"invoice_number": "INV-001",
"total_amount": "$299.99",
"line_items": [
{
"description": "Web Development",
"unit_price": "$200.00"
},
{
"description": "Design Services",
"unit_price": "$99.99"
}
]
}
}`
req, _ := http.NewRequest("POST", "https://api.pdfbolt.com/v1/direct", bytes.NewBufferString(jsonBody))
req.Header.Add("API-KEY", "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")
req.Header.Add("Content-Type", "application/json")
resp, _ := http.DefaultClient.Do(req)
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
fmt.Printf("HTTP %d\n", resp.StatusCode)
body, _ := io.ReadAll(resp.Body)
fmt.Printf("Error Message: %s\n", string(body))
return
}
file, _ := os.Create("invoice.pdf")
defer file.Close()
io.Copy(file, resp.Body)
fmt.Println("PDF generated successfully")
}
Create your first template in the app, then use its ID in your API calls.
The Sync endpoint returns a JSON response with a download URL for the PDF.
➡️ Sources:
- URL
- HTML
- Template
Convert webpage and get download URL:
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
func main() {
jsonBody := `{
"url": "https://example.com",
"format": "A4",
"printBackground": true
}`
req, _ := http.NewRequest("POST", "https://api.pdfbolt.com/v1/sync", bytes.NewBufferString(jsonBody))
req.Header.Add("API-KEY", "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")
req.Header.Add("Content-Type", "application/json")
resp, _ := http.DefaultClient.Do(req)
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
fmt.Printf("HTTP %d\n", resp.StatusCode)
body, _ := io.ReadAll(resp.Body)
fmt.Printf("Error Message: %s\n", string(body))
return
}
body, _ := io.ReadAll(resp.Body)
var result map[string]interface{}
json.Unmarshal(body, &result)
fmt.Printf("PDF URL: %s\n", result["documentUrl"])
}
Convert HTML and get download URL:
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
)
func main() {
htmlContent := "<html><body><h1>Hello!</h1><p>This is a sample PDF.</p></body></html>"
base64Html := base64.StdEncoding.EncodeToString([]byte(htmlContent))
jsonBody := fmt.Sprintf(`{
"html": "%s",
"margin": {
"top": "30px",
"left": "30px"
}
}`, base64Html)
req, _ := http.NewRequest("POST", "https://api.pdfbolt.com/v1/sync", bytes.NewBufferString(jsonBody))
req.Header.Add("API-KEY", "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")
req.Header.Add("Content-Type", "application/json")
resp, _ := http.DefaultClient.Do(req)
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
fmt.Printf("HTTP %d\n", resp.StatusCode)
body, _ := io.ReadAll(resp.Body)
fmt.Printf("Error Message: %s\n", string(body))
return
}
body, _ := io.ReadAll(resp.Body)
var result map[string]interface{}
json.Unmarshal(body, &result)
fmt.Printf("PDF URL: %s\n", result["documentUrl"])
}
Use templates and get download URL:
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
func main() {
jsonBody := `{
"templateId": "your-template-id",
"templateData": {
"client_name": "John Doe",
"invoice_number": "INV-001",
"total_amount": "$299.99",
"line_items": [
{
"description": "Web Development",
"unit_price": "$200.00"
},
{
"description": "Design Services",
"unit_price": "$99.99"
}
]
}
}`
req, _ := http.NewRequest("POST", "https://api.pdfbolt.com/v1/sync", bytes.NewBufferString(jsonBody))
req.Header.Add("API-KEY", "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")
req.Header.Add("Content-Type", "application/json")
resp, _ := http.DefaultClient.Do(req)
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
fmt.Printf("HTTP %d\n", resp.StatusCode)
body, _ := io.ReadAll(resp.Body)
fmt.Printf("Error Message: %s\n", string(body))
return
}
body, _ := io.ReadAll(resp.Body)
var result map[string]interface{}
json.Unmarshal(body, &result)
fmt.Printf("PDF URL: %s\n", result["documentUrl"])
}
The Async endpoint processes requests asynchronously and sends results via webhook.
➡️ Sources:
- URL
- HTML
- Template
Convert webpage with webhook notification:
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
func main() {
jsonBody := `{
"url": "https://example.com",
"format": "A4",
"printBackground": true,
"webhook": "https://your-app.com/webhook"
}`
req, _ := http.NewRequest("POST", "https://api.pdfbolt.com/v1/async", bytes.NewBufferString(jsonBody))
req.Header.Add("API-KEY", "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")
req.Header.Add("Content-Type", "application/json")
resp, _ := http.DefaultClient.Do(req)
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
fmt.Printf("HTTP %d\n", resp.StatusCode)
body, _ := io.ReadAll(resp.Body)
fmt.Printf("Error Message: %s\n", string(body))
return
}
body, _ := io.ReadAll(resp.Body)
var result map[string]interface{}
json.Unmarshal(body, &result)
fmt.Printf("Request ID: %s\n", result["requestId"])
fmt.Println("PDF will be sent to webhook when ready")
}
Convert HTML with webhook notification:
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
)
func main() {
htmlContent := "<html><body><h1>Hello!</h1><p>This is a sample PDF.</p></body></html>"
base64Html := base64.StdEncoding.EncodeToString([]byte(htmlContent))
jsonBody := fmt.Sprintf(`{
"html": "%s",
"margin": {
"top": "30px",
"left": "30px"
},
"webhook": "https://your-app.com/webhook"
}`, base64Html)
req, _ := http.NewRequest("POST", "https://api.pdfbolt.com/v1/async", bytes.NewBufferString(jsonBody))
req.Header.Add("API-KEY", "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")
req.Header.Add("Content-Type", "application/json")
resp, _ := http.DefaultClient.Do(req)
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
fmt.Printf("HTTP %d\n", resp.StatusCode)
body, _ := io.ReadAll(resp.Body)
fmt.Printf("Error Message: %s\n", string(body))
return
}
body, _ := io.ReadAll(resp.Body)
var result map[string]interface{}
json.Unmarshal(body, &result)
fmt.Printf("Request ID: %s\n", result["requestId"])
fmt.Println("PDF will be sent to webhook when ready")
}
Use templates with webhook notification:
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
func main() {
jsonBody := `{
"templateId": "your-template-id",
"templateData": {
"client_name": "John Doe",
"invoice_number": "INV-001",
"total_amount": "$299.99",
"line_items": [
{
"description": "Web Development",
"unit_price": "$200.00"
},
{
"description": "Design Services",
"unit_price": "$99.99"
}
]
},
"webhook": "https://your-app.com/webhook"
}`
req, _ := http.NewRequest("POST", "https://api.pdfbolt.com/v1/async", bytes.NewBufferString(jsonBody))
req.Header.Add("API-KEY", "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")
req.Header.Add("Content-Type", "application/json")
resp, _ := http.DefaultClient.Do(req)
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
fmt.Printf("HTTP %d\n", resp.StatusCode)
body, _ := io.ReadAll(resp.Body)
fmt.Printf("Error Message: %s\n", string(body))
return
}
body, _ := io.ReadAll(resp.Body)
var result map[string]interface{}
json.Unmarshal(body, &result)
fmt.Printf("Request ID: %s\n", result["requestId"])
fmt.Println("PDF will be sent to webhook when ready")
}
3. What's next?
Explore how PDFBolt can streamline your Go applications. Start with the API Endpoints and dive into the Conversion Parameters to customize your integration for your specific requirements.