I am reporting https://www.drupal.org/node/2706755 to moderators because:

I am trying to use your slick Entity Print program for programmatically generating and saving PDF files directly to a content type. Do you have a better example? Anyway, here is my persistent error message regardless of the headers that I have tried using:

GuzzleHttp\Exception\TooManyRedirectsException: Will not follow more than 5 redirects in GuzzleHttp\RedirectMiddleware->guardMax() (line 141 of vendor\guzzlehttp\guzzle\src\RedirectMiddleware.php).

GuzzleHttp\RedirectMiddleware->checkRedirect(Object, Array, Object) (Line: 70)
GuzzleHttp\RedirectMiddleware->GuzzleHttp\{closure}(Object) (Line: 39)
GuzzleHttp\Promise\FulfilledPromise::GuzzleHttp\Promise\{closure}() (Line: 61)
GuzzleHttp\Promise\TaskQueue->run(1) (Line: 246)
GuzzleHttp\Promise\Promise->invokeWaitFn() (Line: 223)
GuzzleHttp\Promise\Promise->waitIfPending() (Line: 266)
GuzzleHttp\Promise\Promise->invokeWaitList() (Line: 225)
GuzzleHttp\Promise\Promise->waitIfPending() (Line: 62)
GuzzleHttp\Promise\Promise->wait() (Line: 129)
GuzzleHttp\Client->request('get', 'http://localhost/sunland/entityprint/node/73', Array) (Line: 87)
GuzzleHttp\Client->__call('get', Array) (Line: 356)
GuzzleHttp\Client->get('http://localhost/sunland/entityprint/node/73', Array) (Line: 356)
Drupal\sunland_actions\Form\SendMultipleConfirmForm->submitForm(Array, Object)

Here is my code:

        $node = \Drupal\node\Entity\Node::load($entity->id());

        $dataSource = sprintf("http://localhost/sunland/entityprint/node/%d", $entity->id());
        $dataTarget = sprintf("private://%s-%s", date("Y"), date("m"));
        $dataTitle = sprintf("Released: %s", $entity->label());

        // Create file object from remote URL.
        // Drupal 7: drupal_http_request() is an alternative to file_get_contents()
        // Drupal 8: \Drupal::httpClient()->get() is an alternative to drupal_http_request() is an alternative to file_get_contents()
        $response = \Drupal::httpClient()->get($dataSource, ['headers' =>
            ['Content-type' => 'application/pdf'],
        ]);

        $data = (string) $response->getBody();
        
        $file = file_save_data($data, $dataTarget);

        // Create node object with attached file.
        $newNode = Node::create([
            'type'        => 'file',  // content type
            'title'       => $dataTitle,
            'field_file' => [
              'target_id' => $file->id(),
            ],
        ]);
        
        $newNode->save();

Comments

frmcclurg created an issue. See original summary.

apaderno’s picture

Category: Task » Support request
Status: Active » Fixed

This issue queue is for reporting errors in the documentation. If you are asking about your code, you should ask to the forums.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.